I need help with the Conditionals 3 skill test

Good day!
I was working on an assessment and i found myself really stuck in converting an “if else” statement that checks for number ranges into a switch statement.

I have tried checking the console for any errors but i found none. I also tried running the switch statement on the console but it was returning undefined. And to be truthful i did ask ChatGPT to convert my previously made if else statement to a switch statement and it returned a result completely identical to my result.

Please i would please like you to help me assess the bug in my code at https://codepen.io/tolu1123/pen/xxyJrXq.

This is the url to the assessment page (Conditionals 3)–https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Test_your_skills:_Conditionals

I will really appreciate it if i am shown the way out of this dilemma.

Thank you.
Tolu.

Hi @toluene1123 and welcome to the community :wave:

In your example the switch expression evaluates to 75 and this gets (strictly) compared to boolean values (true or false) in the cases. Therefore, no case ever matches and the default case (without any text) will be used.
If you change switch (score) to switch (true) your code will work. When using 75 the following case will evaluate to true: case (score >= 70 && score <= 89). This will match the true in the switch expression and every other case will be false and won’t match.

I hope my explanations are more or less clear. If not, just ask. :slightly_smiling_face:

Conclusion: mikoMK > ChatGPT :wink:

Have a nice day,
Michael

PS: Your email address is visible in your post. It ended up in the optional “name” field of your profile. I recommend removing it.

1 Like

Thank you so much…
It worked…
I really appreciate…
i have read the article and i skimmed through the reference page on switch but did not find the solution you suggested.
Can you please give me some suggestions on how to become a better JavaScript programmer?..I would like your say on that topic…because i really enjoyed CSS.

Along the way “true” which you put in the switch expression. Does the JavaScript engine check the cases (in the switch) first to know what it is searching for, then look for the previous declaration of score inorder to work…

I also want to ask a question sir (About the MDN community). Can i feel free to share my views on any topic or probably if i build up a web application, could i show the game to the community and ask if they could help me assess the code for the application…

Thank you so much Sir. I really appreciate you being there for starters like us.

PS: I have changed my name…Or should i remove it totally?

I’m glad my explanations helped you.

It’s a bit of an unusual usage of the switch statement. Normally, you’ll see and use examples like at the top of the switch article where you have a variable as the switch expression and constant values in the cases. This exercise shows how flexible the switch statement is.

Working through the MDN JS courses is certainly a great start. Beside from that I think it’s important to be curious. When you see a JS method that you don’t know or don’t understand, read its MDN article, try out the examples, change them to see if the code behaves like you think it should.
When you are comfortable with the basics of JS, the next step would be to read about and experiment with front-end frameworks like ReactJS, Vue.js or Svelte. There are introductions to these on MDN as well. These will help you build (interactive) websites.

First, it calculates the switch expression and then it will calculate one case after the other until it finds one that matches the switch expression. When looking at our example the steps are as follows:

  • Calculate the switch expression: Nothing to do because it’s already the value true.
  • Calculate first case: (score < 0 || score > 100) becomes false because the score is 75.
  • Compare the switch expression to the first case: true === false evaluates to false, therefore the code of the first case won’t be executed.
  • Same thing for cases two, three and four.
  • Calculate fifth case: (score >= 70 && score <= 89) becomes true because score is between these values.
  • Compare the switch expression to the fifth case: true === true evaluates to true, therefore the code of the fifth case will be executed.
  • The browser sees the break statement and leaves the switch.

The same goes for more classic examples like the one at the top of the the switch article:

const expr = 'Papayas';
switch (expr) {
  case 'Oranges':
    console.log('Oranges are $0.59 a pound.');
    break;
  case 'Mangoes':
  case 'Papayas':
    console.log('Mangoes and papayas are $2.79 a pound.');
    // Expected output: "Mangoes and papayas are $2.79 a pound."
    break;
  default:
    console.log(`Sorry, we are out of ${expr}.`);
}
  • The switch expression evaluates to 'Papayas'.
  • 'Papayas' === 'Oranges' :arrow_right: false.
  • 'Papayas' === 'Mangoes' :arrow_right: false.
  • 'Papayas' === 'Papayas' :arrow_right: true.
  • The code of the third case will be executed.

Yes, this is fine. Personally, I can’t guarantee that I have the time to look at it, but maybe someone else can help out.

It’s my pleasure to help you.

This is fine.

See you,
Michael

Thank you very much sir.Now i can say i understand switch statement.

Blockquote
When you are comfortable with the basics of JS,the next step would be to read about and experiment with front-end frameworks like ReactJS, Vue.js or Svelte.

Please do you mean that once i am through with the tutorial under " For complete beginners". I should feel free to move to learning about JavaScript frameworks.

I do appreciate your timely response.
Thank you so much.

Yes, that sounds about right :+1:

1 Like