Parrotmaster
Parrotmaster

Reputation: 676

Javascript switch always default

I am working on a switch that sets 2 variables based on the value like so:

switch(_drukte) {
    case "normaal": 
        _gradients.push("green");
        _widths.push("20%");
    break;
    case "drukker": 
        _gradients.push("yellow");
        _widths.push("40%");
    break;
    case "gezellig druk": 
        _gradients.push("orange");
        _widths.push("60%");
    break;
    case "druk": 
        _gradients.push("red");
        _widths.push("80%");
    break;
    case "vol": 
        _gradients.push("full");
        _widths.push("100%");
    break;
    default:
        _gradients.push("green");
        _widths.push("20%");
    break;
}

For some reason this ALWAYS calls the default. _drukte is from an AJAX get on a JSON file and the value changes with a loop. I've alerted the value of _drukte and it is as expected (currently it's "normaal", "drukker", "gezellig druk", "druk" and "vol"). I tried messing up the other cases (even making it invalid javascript) and it just calls the default.

Anybody know why?

Upvotes: 0

Views: 2511

Answers (1)

jAndy
jAndy

Reputation: 236092

I don't want to lean too far out of the window, but I would almost guarantee you that the return value from your ajax request does not fit 100% your case'es.

If I were you, I'd directly console.log( _drukte ); before that switch statement.

Maybe white-spaces ? Capital letters ?

Thats the best answer I can provide without seeing your actual code, so try to figure out which exact value is contained by _drukte when the switch statement gets encountered.

Upvotes: 2

Related Questions