Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
64 views
in Technique[技术] by (71.8m points)

javascript - Switch...case in JS

This simple problem gives me an error. Does not get the correct answer. I will be glad if you help.

let point = 90;
switch (point) {
    case point >= 51 && point <= 60:
        console.log('Your price: E');
        break;
    case point >= 61 && point <= 70:
        console.log('Your price: D');
        break;
    case point >= 71 && point <= 80:
        console.log('Your price: C');
        break;
    case point >= 81 && point <= 90:
        console.log('Your price: B');
        break;
    case point >= 91 && point <= 100:
        console.log('Your price: A');
        break;
    default:
        console.log('You did not pass');
}

Output: You did not pass

question from:https://stackoverflow.com/questions/65649146/switch-case-in-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

this way

let point = 90;
switch (true) {
    case point >= 51 && point <= 60:
        console.log('Your price: E');
        break;
    case point >= 61 && point <= 70:
        console.log('Your price: D');
        break;
    case point >= 71 && point <= 80:
        console.log('Your price: C');
        break;
    case point >= 81 && point <= 90:
        console.log('Your price: B');
        break;
    case point >= 91 && point <= 100:
        console.log('Your price: A');
        break;
    default:
        console.log('You did not pass');
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...