Help with Conditionals 3 test SWITCH statements please?

What were other solutions everyone else got? As I have tried different variations but can’t seem to turn my inner IF statements into successful SWITCH ones:

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

// Add your code here

if (machineActive==true)
{if (score<0 && score>100) {
response='error';}
else if(score>=0 && score<=19){
response='total fail';}
 else if(score>=20 && score<=39){
response='needs work';}
else if(score>=40 && score<=69){
response='not bad';}
else if(score>=70 && score<=89){
response='great';}
else if(score>=90 && score<=100){
response='perfect';}
}
else{response='switch machine on';}

// Don't edit the code below here!

Hello @ayanda

hope that everything going fine with you

check the following


if(machineActive) {
	// Add your code here

switch(true){
case (score<0 && score>100):
response='error';
break;
case (score>=0 && score<=19):
response='total fail';
break;
case (score>=20 && score<=39):
response='needs work';
breaks;
case (score>=40 && score<=69):
response='not bad';
break;
case (score>=70 && score<=89):
response='great';
break;
case (score>=90 && score<=100):
response='perfect';
break;
}

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

hope it help and have a nice day :slight_smile:

This worked :astonished:how did you manage to do it? When I did it, it never worked. Perhaps I hadn’t arranged the code well or there was some typo i missed?

i also stuck with that one but chris helped me

it was a tricky way for the switch statement

not typo the trick to make the switch look for statement that true so it compare the true value to the result value of the statment in each case

hope that help and have a nice day :slight_smile:

1 Like

Also… i think i may have not added brackets…

maybe i missed that i am not sure :slightly_smiling_face:

Thank you so much. I had been stuck on this all day yesterday… i def will need to oay attention to the details when I finish these courses

you very welcome and we all miss something some times

but you doing great well done and have a nice day :slightly_smiling_face:

Thanks for helping out here @justsomeone! I love the passion this community has for helping newcomers :wink:

@ayanda welcome! You’ve been given a pretty good answer here; Also refer to my comment here for a bit more information on why this switch statement example works like it does. In short, switch statements are a bit tricky and annoying to understand when you first meet them, but don’t worry — you’ll get there.

thanks for you @chrisdavidmills :wink:

Perfect! Reading it now. Thank you :slight_smile:

There is an error in the logic. It can not be both less than 0 AND bigger than 100 (score<0 && score>100)

It should be OR i.e. score<0 || score>100

Here is an example of the same process with simpler logic.

let response;

switch(true) {

  case !machineActive:
    response = 'The machine is turned off. Turn it on to process your score.'
    break;

  // the rest are if(machineActive) is true

  // My personal preference is that, 
  // simple case on a single line makes it easier to read

  case score < 0 || score > 100: response = 'error'; break; // bad value

  // at the moment, we know it is between 0 and 100
  // a reverse order makes the logic simpler

  case score >= 90: response = 'perfect'; break;
  case score >= 70: response = 'great'; break;
  case score >= 40: response = 'not bad'; break;
  case score >= 20: response = 'needs work'; break;

  default: response = 'total fail';
}

‘AND’ works fine as well. Did your code get the desired output?

you are right @erosman thanks for letting us know about that and have a nice day :slight_smile: