tags:

views:

96

answers:

4

Hi i am trying to handle an ajax json response

here is my code

success: function (j) {     
    switch(true)
    {
        case (j.choice1):

            alert("choice2");
        break;
        case (j.choice2):
                alert("choice2");
        break;
        default:
            alert("default");
        break;
    }
}

based on what j is return i do my action BUT i keep getting the default.

I have alert the j values and come correct.Some how case (j.choice1) case (j.choice2) is not working.

I tried case (j.choice1!="") (j.choice2!="") But in this scenario i keep getting the first choice.

What am i missing

+5  A: 

You need to read up on the switch statement. You should not be switching on a constant value.

It appears that you need to use if statements, as you don't really want to be switching on your j value:

success: function (j) {     
    if (j.choice1)
    {
        alert("choice1");
        return;
    }

    if (j.choice2)
    {
        alert("choice2");
        return;
    }

    alert("default");
  }
}
Oded
Just curious: why should one not switch on a constant value?
Tim Down
It will provide the same result every time. It's somewhat akin to `if(x = y) condition`
Lord Torgamus
@Lord Torgamus: technically, it would only be similar if `y` was always truthy or always falsey.
Matthew Crumley
@Matthew: I consciously decided not to go for that level of detail, but yes, you are correct.
Lord Torgamus
@Lord Torgamus: I figured that. I just like to be pedantic sometimes, although, I guess "somewhat" kind of covers you there.
Matthew Crumley
I'm failing to see the real problem with using `switch` like this. The task is to find only the first occurrence of `true` in a short, pre-defined list of values, which the `switch` statement achieves correctly. The only other sane looking option that occurs to me is using `if/else if`. The `switch` statement to me is just as readable as `if/else if` and works just as well, so unless there's a performance issue (and I can't see why there would be), I don't see a problem.
Tim Down
@Tim Down - it is a very obscure usage and not apparent at first glance. I'd rather have my code be readable.
Oded
Oded: Fair enough, I agree it is uncommon and therefore requires more time to read and understand than it warrants.
Tim Down
+2  A: 

It works for me:

var a = 0, b = true;

switch(true) {
    case a:
        alert('a');
        break;
    case b:
        alert('b');
        break;
}

However, the case labels must be equal to true, not jut implicitly true.
Also, only the first case that evaluates to true will execute.

SLaks
A: 

In a case like this, a better way to do this is probably something like:

success: function (j) {
    if(j.choice1 || j.choice2) {
        alert("choice2");
    } else {
        alert("default");
    }
}
Syntactic
A: 

SOLVED

Based on SLaks answer i modify the code as below

    if(j.choice1){ var choice1=true;} else { var choice1=false;}
    if(j.choice2){ var choice2=true;} else { var choice2=false;}

    switch(true)
    {
        case choice1:
            alert("choice1");
        break;
        case choice2:
            alert("choice2");
        break;
        default:
            alert("default");
        break;
    }

For all asking why switch and not if.

Switch will execute only 1 statement, but if can execute more than 1 if any mistake come form response (for example if set choice1 and choice 2 the if will alert both but switch will alert only choice1).

The response expecting as choice has to do with credit card charge to bank so i want to ensure that only 1 action will exetute

Thank to all

ntan