0

Need help fixing Javascript code

So the goal is to create a "Guess my number" between 1 - 5. I thought everything was correct but apparently JS is not accepting any inputs and jumping straight to the default. Can someone explain where I went wrong? https://code.sololearn.com/W5xI7ryIwCXK/#js

18th Nov 2019, 10:26 PM
Prinn
Prinn - avatar
3 odpowiedzi
+ 2
Prinn There are two problems: 1. Prompt returns a string and the switch statement will evaluate the expression(string from prompt), using strict comparison (===). You need to convert the string to a number, you can use: Number(a), parseInt(a), or +a. 2. You are trying to use expressions for each case when you should be using an integer for each case, ex. case 1: case 2: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
18th Nov 2019, 11:46 PM
ODLNT
ODLNT - avatar
0
This is not how you use the switch statement, you put it as id it was an if/else statement. In switch statement, you don't check conditions, you check the exact value, for example, you can't check if a is bigger or smaller than another number, here is how to: switch(a){ case 1: .... What I mean by that, is that you check if it is exactly equal to your case, all this is well explained in the course, please check it next time before posting such questions.
18th Nov 2019, 11:36 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
I don't think you can use a switch like that, you need an if /else if statement. or this document.write(a == e ? "That's my number": a <= d ? "nope, too low": a >= f ? "nope, too high": "you havent guessed");
18th Nov 2019, 11:36 PM
rodwynnejones
rodwynnejones - avatar