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!
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.';
}
Thanks for helping out here @justsomeone! I love the passion this community has for helping newcomers
@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.
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';
}