coding style - Is it possible to assign a code block to a switch case twice in Javascript? -


i have javascript processing form send results server through ajax, , want reuse code process common components of question types. let's have 2 question types "forced choice" , "forced choice other", both of use radio buttons force user choose 1 option, in second type, radio button labeled "other" allow them input own answer question box. getting radio button value common between 2 getting "other" value not. acceptable say

switch(questiontype) {     case 'forced choice':     case 'forced choice other':         //code radio button value     case 'forced choice other':         //if value "other", value of textbox         break;     default:         break; } 

or preferable

switch(questiontype) {     case 'forced choice':     case 'forced choice other':         //code radio button value         if(val == 'other' && questiontype == 'forced choice other')             //code "other" value         break;     default:         break; } 

edit: wanted add clarification in actual code, question type distinction not simple or without allowing "other". i'd use same code value of radio-button based questions, , there several types. some, simple getting values, others, script have check how script configured in form's json configuration file. i'd use first pattern avoid chunk of several if statements have 2-3 conditions.

which pattern think preferable , why (if first pattern function correctly)?

in javascript, in lot of common languages, switch cases "fall through" next 1 unless broken explicitly break statement. means first example work, technically, it's terrible practice, confusing read , maintain (also, wouldn't surprised if duplicate label confused interpreters or code analyzers).

it's best extract common functionality named functions. make code easiest read , maintain, , repetition reduced calling same function twice.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -