1

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.';
}




dnewbie25
  • 25
  • 5

3 Answers3

3

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"
}
Mike
  • 555
  • 3
  • 14
  • 1
    "the switch statement is used to determine if any of the cases equal the variable passed in" <- I actually think this is much clearer than the top answer in linked duplicate question because it explicitly points out the source of misunderstanding at the very beginning. Thanks @Mike! – Casey L Aug 05 '20 at 16:55
1

Change switch(score){ to switch(true){

It's trying to equate (score<=100 && score>=90) === score which it does not.

Seph Reed
  • 8,797
  • 11
  • 60
  • 125
1

You need

switch (true) {

because you are checking against a boolean value.

By using score, the only part with

case score:

would work, because switch uses a strict comparison.

Then you could omit all parenteses and use a fall through by strating with filte for off values above, folloewd by smaller value and finally by having a default value for negative values.

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

if (machineActive) {
    switch (true) {
        case score > 100:
            response = "Something must be wrong";
            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";
    }
} else {
    response = 'The machine is turned off. Turn it on to process your score.';
}

console.log(response);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392