Help with Conditionals 3

Hello, I have been taking in these JS lessons today and stuck on one of the assessments on conditionals at the bottom of the page.

Link to Assessment

Above is a link to the assessment. It is #3 at the bottom of the page.

Below is the code plus my switch statement that I cant get to return any value in response at all.
Can someone please tell me what I am doing wrong and point me in the right direction?

let response;
let score = 1;
let machineActive = true;

if(machineActive) {

switch (score) {
case score > 0:
response = “Nice!”;
break;
}

}

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

// Don’t edit the code below here!

section.innerHTML = ’ ';
let para1 = document.createElement(‘p’);
let para2 = document.createElement(‘p’);

para1.textContent = Your score is ${ score }.;
para2.textContent = response;

section.appendChild(para1);
section.appendChild(para2);

Hey buddy,

It took me a bit of time to figure this one out as well, but from looking at your code, I can see a few things. Hopefully this link should help
StackOverFlow: Using conditionals in switch statements , also I had to dig around the forum, but it looks like the switch(expression) has to be set to switch(true){ case (put the logic in brackets)://do shizz; break;

Hope this helps
=)

1 Like

@drgaud yup, you are on the right track here!

@Nathaniel_Rod switch statements are a pain. Have another go at completing this one, following @drgaud 's advice. If you can’t get it, have a look at our answer, then go back and try to implement it yourself.

3 Likes

@chrisdavidmills ~ apologies for the 2-year-old thread revival! but…

I’ve got working code for this exercise but it’s different than yours, and when I try to make my code mimic yours (remove the parenthesis around each singular comparison in each case, so it’s one expression), it breaks and only shows the default…
Of note, I selected a different range (90-100) to be default - but I feel like that shouldn’t matter if the comparisons are correct?


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

if(machineActive) {
// Add your code here
switch (true) {
case (score < 0) || (score > 100):
response = “This is not possible, an error has occurred.”;
break;

case (score <= 19):
response = “That was a terrible score — total fail!”;
break;

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

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

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

default:
response = “What an amazing score! Did you cheat? Are you for real?”;
}
} else {
response = ‘The machine is turned off. Turn it on to process your score.’;
}


So the above works, but if I edit the parenthesis structure to mimic yours, ie:

case (score => 70) && (score <= 89):
becomes
case (score => 70 && score <= 89):

I get the default response no matter the score, and I’m quite stumped as to why?
Many thanks in advance!

Hi @dagber and welcome to the community :wave:

You accidentally created an arrow function (=>) returning 70 instead of using the “greater than or equal” operator (>=). This leads to different results when setting parentheses because of the operator precedence.

Have a nice day,
Michael

@mikoMK - major thanks. Went out of town shortly after posting & just getting back into this, complete with a full-blown head smack :sweat_smile:

I do appreciate that it’s a simple fix & bet I don’t make that mistake again! Thank you!

1 Like

Making mistakes is a great way to learn :grin: