Switch statements in Javascript return the default every time, but not the cases

dnewbie25 :

I was trying to solve the problem Conditionals 3 from the Mozilla Foundation website. The thing is, the activity asks to use only Switch statements inside the if(machineActive). I solved it using if-else, but once I tried to use the switch statement the console shows the default message I set, "Something must be wrong", no matter what value the score variable gets. Any time I change the cases just the default message is being shown.

What's the correct way to use switch statements in this case? The if-else is the most appropriate choice, but I want to stick with the task's rules this time.

let response;
let score = 75;
let machineActive = true;

if(machineActive) {


    switch(score){

        case (score<=100 && score>=90):
            response = "What an amazing score! Did you cheat? Are you for real?";
        break;

        case (score<=89 && score>=70):
            response = "That\'s a great score, you really know your stuff.";
        break;


        case (score<=69 && score>=40):
            response = "You did a passable job, not bad!";
        break;


        case (score<=39 && score>=20):
            response = "You know some things, but it's a pretty bad score. Needs improvement.";
        break;

        case (score<=19 && score>=0):
            response = "That was a terrible score — total fail!";
        break;

        default:
            response = "Something must be wrong";

    }


} else {
  response = 'The machine is turned off. Turn it on to process your score.';
}




Mike :

the switch statement is used to determine if any of the cases equal the variable passed in. you are passing in a number, so none of your boolean cases will equal the number. you're going to want to pass in true and see if any cases evaluate to true.

A case clause used to match against expression. If the expression matches the specified valueN, the statements inside the case clause are executed until either the end of the switch statement or a break. from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

Also, the cases are evaluated in order, and since you have breaks you don't need to check the upper end of the range on every case

switch (true) {
  case score > 100: 
    response = "You definitely cheated. You must have gone to https://www.stackoverflow.com to get answers to all these questions."
    break

  case score >= 90:
    response = "What an amazing score! Did you cheat? Are you for real?"
    break

  case score >= 70:
    response = "That's a great score, you really know your stuff."
    break

  case score >= 40:
    response = "You did a passable job, not bad!"
    break

  case score >= 20:
    response =
      "You know some things, but it's a pretty bad score. Needs improvement."
    break

  case score >= 0:
    response = "That was a terrible score — total fail!"
    break

  default:
    response = "Something must be wrong"
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=29811&siteId=1