Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a switch statement inside another switch statement

Im trying to work out a quote value after a user makes a selection in 2 select boxes.

It looks like 1 need 2 switch statements. Is this possible? (There are a lot more possible values than the ones below. I've deleted them for the sake of clarity)

 var workOutQuote = function() {
        var value1 = $("#delivery_from_area").val();
        var value2 = $("#delivery_to_area").val();
        var val = '';
            switch (value1)
            { case '1':
                { switch (value2)
                    { case '1': val="90"; break; }
                    { case '2': val="80"; break; }
                    { case '3': val="70"; break; }
                }
            }
            { case '2':
                { switch (value2)
                    { case '1': val="80"; break; }
                    { case '2': val="90"; break; }
                    { case '3': val="70"; break; }
                }
            }
}

Using the code above I get the error:

Uncaught SyntaxError: Unexpected token case 
like image 993
grabury Avatar asked Oct 25 '13 07:10

grabury


2 Answers

Use BREAK , if you don't want to call all case. Otherwise ,you will get unexpected Results

 var workOutQuote = function() {
        var value1 = $("#delivery_from_area").val();
        var value2 = $("#delivery_to_area").val();
        var val = '';
            switch (value1){ 
            case '1':
                 switch (value2){
                     case '1': val="90"; break; 
                     case '2': val="80"; break; 
                     case '3': val="70"; break; 
                }

            break;
            case '2':
                 switch (value2){
                     case '1': val="80"; break; 
                     case '2': val="90"; break; 
                     case '3': val="70"; break; 
                }
                break;             
}
like image 61
Prateek Avatar answered Sep 20 '22 01:09

Prateek


You are putting case in braces, an example from your code { case '1': val="90"; break; }

    var value1 = '2';
    var value2 = '2';
    var val = '';
    switch (value1) {
        case '1':
            {
                switch (value2) {
                    case '1':
                        val = "90";
                        break;

                    case '2':
                        val = "80";
                        break;

                    case '3':
                        val = "70";
                        break;
                }
            }
        case '2':
            {
                switch (value2) {
                    case '1':
                        val = "80";
                        break;
                    case '2':
                        val = "90";
                        break;
                    case '3':
                        val = "70";
                        break;
                }
            }
    }

Demo

like image 36
Satpal Avatar answered Sep 21 '22 01:09

Satpal