+ 1
Why is my switch only displaying the default loop? Made a BMI calculator
https://code.sololearn.com/W9G91Tav0f62/?ref=app This is the code. Why is the BMI calculator only displaying the default statement no matter the value? Units used are kg and cm
4 Answers
+ 6
With the "switch/case" statement, you only can test equality of differents values:
switch (imc) {
case 5: document.write('imc is 5');
case 15: document.write('imc is 15');
case 25: document.write('imc is 25');
}
... by putting conditions after the 'case' keyword, you are comparing the 'imc' value to the result of the condition:
"case xxx: /*do something*/" is equivalent to "if (imc /*switch tested variable*/ == xxx { /*do something*/ }"
So, while imc is different from true or false, only the default case run ^^
+ 6
However, isn't it strange to user input in french ( "ton poids", "ta taille"), and output in english ;P ?
0
Thanks a lot! Rewrote it with else if and it works perfectly
0
I'm weird, sorry! Thanks again :)