Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I am using Switch statement in JavaScript,I need to apply single case on multiple cases

I am writing code as:

C#
function SelectDropDownValue(Service_Name, DefaultValue, Fieldname) {
    var result;
    switch (Service_Name) {      
        case ("SUBDIVIDED" || "ARB" || "MINING CLAIM" || "SECTIONAL" || "MAPS AND SUBDIVISIONS"):
            if (DefaultValue == "N" && Fieldname == "BACKTOBASEORDEROPTION") {
                result = "PLANT_BEGINNING";
                break;
            }
            else if (DefaultValue == "1") {
                result = "MOST_RECENT";
                break;
            }
            else if (DefaultValue == "2") {
                result = "SECOND_MOST";
                break;
            }
            else if(DefaultValue == "N" && Fieldname=="SORTINVESTIGATIVEOPTION" || Fieldname=="SORTORDEROPTION") 
            {
            result ="Newest";
            break;
            }//End       
        default: result = ""; break;
    } //end switch case
    return result;
}


Problem is arising is that the case is running only for Subdivided and not for other values

How can i do this in JS?
Posted

C#
function SelectDropDownValue(Service_Name, DefaultValue, Fieldname) {
    var result;
    switch (Service_Name) {
        case ("SUBDIVIDED"): 
        case ("ARB"): 
        case ("MINING CLAIM"): 
        case ("SECTIONAL"): 
        case ("MAPS AND SUBDIVISIONS"):
            if (DefaultValue == "N" && Fieldname == "BACKTOBASEORDEROPTION") {
                result = "PLANT_BEGINNING";
                break;
            }
            else if (DefaultValue == "1") {
                result = "MOST_RECENT";
                break;
            }
            else if (DefaultValue == "2") {
                result = "SECOND_MOST";
                break;
            }
            else if(DefaultValue == "N" && Fieldname=="SORTINVESTIGATIVEOPTION" || Fieldname=="SORTORDEROPTION")
            {
            result ="Newest";
            break;
            }//End
        default: result = ""; break;
    } //end switch case
    return result;
}


JavaScript uses C like syntax for switch statement as shown above and so just using a newline and another case should work fine.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 5-Apr-11 3:50am    
I see you beat me to it! 5+
This is the abuse of switch statement: it makes absolutely no sense if there is only one case + default. You also mix it with "if" statement. Change it all to "if", don't torture readability and common sense.

—SA
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 5-Apr-11 3:51am    
You're right SA! 5+
Thanks for pointing that out.
Sergey Alexandrovich Kryukov 5-Apr-11 3:55am    
Thank you, Manfred.
--SA
Guyverthree 5-Apr-11 3:56am    
Indeed you are right and readability of code is important.
Sergey Alexandrovich Kryukov 5-Apr-11 10:59am    
Thank you, Safar,
--SA
You'll have to do it like this:

C#
function SelectDropDownValue(Service_Name, DefaultValue, Fieldname) {
    var result;
    switch (Service_Name) {
        case "SUBDIVIDED":
        case "ARB": 
        case "MINING CLAIM":
        case "SECTIONAL":
        case "MAPS AND SUBDIVISIONS":
            if (DefaultValue == "N" && Fieldname == "BACKTOBASEORDEROPTION") {
                result = "PLANT_BEGINNING";
            }
            else if (DefaultValue == "1") {
                result = "MOST_RECENT";
            }
            else if (DefaultValue == "2") {
                result = "SECOND_MOST";
            }
            else if(DefaultValue == "N" && Fieldname=="SORTINVESTIGATIVEOPTION" || Fieldname=="SORTORDEROPTION")
            {
                result ="Newest";
            }//End
            break;
        default:
            result = "";
            break;
    } //end switch case
    return result;
}


That way it should work. BTW I moved all of your breaks out from the else-ifs because I like that style better and in this case there really isn't a difference.

Happy coding!
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 5-Apr-11 3:56am    
Looks reasonable, a 5.
Happy coding to you, too!
--SA
Hi, what if you want to check multiple conditions using AND operator:
switch(true)<br />
{<br />
   case (val_1 == "12" && val_2 == "34"):<br />
   //statements;<br />
   break;<br />
}
 
Share this answer
 
v2
Comments
Graham Breach 13-Sep-11 8:11am    
Then you use an "if" and not a "switch/case".

"switch(true)" - ?????

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900