+ 2
Could someome explain me why the code isn't working?
2 Antworten
+ 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😄
+ 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).