5987 Shares

Can anyone tell me what is wrong with my JS code?

Can anyone tell me what is wrong with my JS code? Topic: Case switch javascript string
June 25, 2019 / By Leyton
Question: var con1,con2,con3,con4; function question1() { con1=prompt("What's the size of the ram in your computer?\n1. 1gb\n2. 2gb\n3. 4 gb\n4. 8gb"); } question1(); switch(con1) { case 1: alert("Wrong answer."); break; case 2: alert("Wrong answer."); break; case 3: alert("You're right! Congrats!"); break; case 4: alert("Wrong answer."); break; } The question shows up. I enter the response and it just stops after that. The switch statement doesn't kick in. I just replaced the 1 with "1" and it worked. But why? I didn't get that. Explanation as to why that happens will be much appreciated.
Best Answer

Best Answers: Can anyone tell me what is wrong with my JS code?

Jareb Jareb | 1 day ago
A prompt returns text. Just because the text happens to be the number one does not mean it's the same thing, one is text and the other is a number. JavaScript has no variable types like int or String (in fact in JavaScript, everything is an object), but that still means that 1 is not the same as "1". For example if you use alert(1 + 1); you'll get 2, but if you use alert("1" + "1"); you'll get "11", because text is simply appended to each other. Like Husayn already explained, you can turn text into a number using the parseInt function: con1 = parseInt( promt(...) ); Provided the player entered an integer, con1 will now be an integer, not text.
👍 90 | 👎 1
Did you like the answer? Can anyone tell me what is wrong with my JS code? Share with your friends

We found more questions related to the topic: Case switch javascript string


Jareb Originally Answered: What is wrong with this c++ code?
The "switch(int)" is wrong, as others have pointed out. You need to specify the variable that you want to compare in the switch (e.g., x) and not a type (e.g. int). Second, your call to system("pause") (bad as that is, for a number of reasons unrelated to this question and with which I won't bore you) will never execute, neither will your 'return 0'. Instead once the switch completes, your execution will simply terminate. Try putting that bit of code *after* the closing curly bracket of the switch().
Jareb Originally Answered: What is wrong with this c++ code?
The int in the switch is only one problem, once you see that there are problems with what you are trying to do, you can only have a single integer put into a switch and x, y and z don't fit as none of them can be equal to 0, 1 or 2. Something like this is appropriate: include #include using namespace std; int main() { int number = rand() % 3; //Creates a random number in the range 0, 1 or 2. switch (number) { case 0: cout << "3"; break; case 1: cout << "4"; break; case 2: cout << "5"; break; return 0; } } Also Main can be both void or int in C++, makes conversion from C easier.
Jareb Originally Answered: What is wrong with this c++ code?
I'm not actually answering your question, but I wanted to point some people to the reference below. It seems to be a mistaken belief that main() can be declared in c++ as void main(). Some compilers do accept that, but it's not compliant with the C++ standard which states the return type must be an int.

Fred Fred
What ever you input in the prompt gets accepted as a string type therefore your cases should be checking strings i.e. case "1" If you want to keep it as is you will need to parseInt con1 to extract the integer from it hence being able to use case 1 Cheers, > Proceed at your own risk <
👍 30 | 👎 0

Fred Originally Answered: Can anyone tell me what is wrong with my JS code?
A prompt returns text. Just because the text happens to be the number one does not mean it's the same thing, one is text and the other is a number. JavaScript has no variable types like int or String (in fact in JavaScript, everything is an object), but that still means that 1 is not the same as "1". For example if you use alert(1 + 1); you'll get 2, but if you use alert("1" + "1"); you'll get "11", because text is simply appended to each other. Like Husayn already explained, you can turn text into a number using the parseInt function: con1 = parseInt( promt(...) ); Provided the player entered an integer, con1 will now be an integer, not text.
Fred Originally Answered: Can anyone tell me what is wrong with my JS code?
What ever you input in the prompt gets accepted as a string type therefore your cases should be checking strings i.e. case "1" If you want to keep it as is you will need to parseInt con1 to extract the integer from it hence being able to use case 1 Cheers, > Proceed at your own risk <

If you have your own answer to the question case switch javascript string, then you can write your own version, using the form below for an extended answer.