简体   繁体   中英

basic javascript , multiple else if giving unexpected output

function getRankForScore(score){
         if(score <= 20)        return 1;
    else if(21 < score <=40)    return 2;
    else if(41 < score <=60)    return 3;
    else if(61 < score <=90)    return 4;
    else return 5;
}

getRankForScore(10) // Returns 1 ,expected

getRankForScore(22) //Returns 2 , expected

But

getRankForScore(50 or any number > 21) // Returns 2 .

How can i use switch case construct for something like inequality checks ? What is wrong with the above function ?

Because

when when you enter number greater than 21 the condition 21 < score evaluates to true and returns 1 which is then compared with 40, which is obviously less than 40

Your syntax was wrong in the else if's:

function getRankForScore(score){
    if(score <= 20)         return 1;
    else if(score <= 40)    return 2;
    else if(score <= 60)    return 3;
    else if(score <= 90)    return 4;
    else return 5;
}

You can use a oneliner here, using short circuit evaluation :

function getRankForScore(score){
 return score <= 20 && 1 || score <=40 && 2 || 
        score <= 60 && 3 || score <=90 && 4 || 5;
}

a < b < c不是您所期望的,它被解释为(a < b) < c ,例如,它变为true < c

function getRankForScore(score) {
    var result;

    switch(true) {
       case score <= 20:
           result = 1;
           break;
       case score > 21 && score <= 40:
           result = 2;
           break;
       case score > 41 && score <= 60:
           result = 3;
           break;
       case score > 61 && score <= 90:
           result = 4;
           break;
      default:
           result = 5;
    }

    return result;
}

Why don't you use a code like:

function getRankForScore(score) {
    if(score <= 20) {
        return 1;
    } else if(score > 21 && score <= 40) {
        return 2;
    } else if(score > 41 && score <= 60) {
        return 3;
    } else if(score > 61 && score <= 90) {
        return 4;
    } else {
        return 5;
    }
}

Try this way:

function getRankForScore(score){
         if(score <= 20)        return 1;
    else if(score >21 && score <=40)    return 2;
    else if(score > 41 && score <=60)    return 3;
    else if(score> 61 && score <=90)    return 4;
    else return 5;
}

The expression if (a < b < c) is not leagal.

That's why:

function getRankForScore(score){
         if(score <= 20)        return 1;
    else if(score > 21 && score <= 40)    return 2;
    else if(score > 41 && score <= 60)    return 3;
    else if(score > 62 && score <= 80)    return 4;
    else return 5;
}

alert(getRankForScore(42));

Javascript sometimes is not math :(

You can't do 21 < score <= 40, you must use && statement.

Working fiddle here: http://jsfiddle.net/BaJse/

I think that when you write something like this:

if(21 < score <=40)

it actually equals to:

if (21 < score || score <= 40)

which fits your condition. of any number bigger than 21 - and that is why it doesn't go on checking your other if statements.

you should do an AND statement:

if (21 < score && score <= 40)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM