+ 2

Could someome explain me why the code isn't working?

https://code.sololearn.com/W70UhPbx4GQ1/?ref=app

18th Jun 2019, 1:30 AM
M. Elle
M. Elle - avatar
2 odpowiedzi
+ 4
The problem in code is when you are taking input using prompt then it returns a string that you are storing in variable c. But in switch case numbers are used with each cases. So you need to change the input from string to integer. For converting a string number to a number I have used a function called parseInt (). Here I have solved your problem in JS: function carbonos() { return prompt("¿Cuántos carbonos tiene la cadena principal? Inserta un número") } var c = parseInt(carbonos()); switch(c){ case 1: document.write("met"); break; case 2: document.write("et"); break; default: document.write("No se puede calcular"); } Hope this helps😄
18th Jun 2019, 2:10 AM
Deepak Kumar
Deepak Kumar - avatar
+ 1
prompt() returns a string. The switch statement works when you replace 1 and 2 with "1" and "2". You could otherwise convert it to an integer with Number(c) or parseInt(c, 10).
18th Jun 2019, 1:58 AM
Cluck'n'Coder
Cluck'n'Coder - avatar