Help wanted for JavaScript assessment conditionals assignment 3

Dear Sir/Madam,

I hope someone can help me with this.
In the third assignment on https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Test_your_skills:_Conditionals
you are supposed to use switch for giving back responses for different scores. However, I keep getting the error: SyntaxError: expected expression, got ‘<’ on the lines where I put (for instance) case <0:
It seems as if it is not allowed to use < or > and if case always just checks if the expressions between the brackets after switch exactly matches (===) the expression after case. I tried a lot of different things, but can’t get rid of this error. Am I doing something wrong in my syntax?
I put my code up at JSfiddle and hope you can have a look at it through this link: https://jsfiddle.net/kf_km/36dgwk1f/1/#&togetherjs=i5lpMDnUEg.
I hope you can help me with this.
Thanks a lot,

Karen

Hi there @kfkm!

You are on the right track in your code. The trouble is, switch statements are pretty confusing, especially in terms of how the conditions are written. So don’t worry that you didn’t get this right the first time round.

The best way to write this solution is to use switch(true), and write the cases out more explicitly. See our solution here: https://github.com/mdn/learning-area/blob/master/javascript/building-blocks/tasks/conditionals/marking.md#task-3

And for an explanation of why this works, see my earlier post here: Assessment wanted Conditionals 3

I hope this helps. If you still need more help, don’t hesitate to ping me.

Best regards.

Thanks a lot. That indeed solved it. :smiley:

Hey there!
why did you used [switch (true)] instead of [switch (score)]??!!
and why did it work??

Hi @AnsBdran

It’s an rather unusual method to use the switch statement, but if we think about the basics of switch/case, it hopefully becomes a bit clearer why it works:

For every case its value gets compared to the switch value. If they are the same, the case code gets executed. Let’s assume the score is 42. Most of the case conditions are false (e. g. score >= 20 && score < 40 :arrow_right: true && false :arrow_right: false). Only one condition is true: score >= 40 && score < 70, because 42 is between these numbers. Since the switch value is true and this case value is also true, the corresponding code gets executed.

Does that make sense?

Have a nice day,
Michael