- 1
Switch code will not work.
You are given a program that takes the number as input. Complete the program so that it will output to the console the theme according to input number. function main() { var themeNumber = parseInt(readLine(), 10) switch(themeNumber){ case "Light": console.log("Light"); break; case "Dark": console.log("Dark"); break; case "Nocturne": console.log("Nocturne "); break; case "Terminal": console.log ("Terminal"); break; case "Indigo": console.log("Indigo "); break; default: console.log("Another color"); }}
11 Respostas
+ 6
Emmanuel Riano
Any reason you are comparing string inside switch..đ€?
+ 1
You read <themeNumber> as an integer by parseInt(), but you compare it to string inside the switch.
0
I honestly thought themenumber was the input expression that will compare the cases and give the output. what is wrong about it?
0
The problem here is, you can't compare numbers to strings, just like you can't compare apples to bananas.
0
Right but whats the number in this situation ive used color names and closest thing to a number is the value themenumber
0
If switch compares the expression to the cases but im trying to get user input by using the variable given. So how am i using a number comparing to a string?
0
function main() {
var themeNumber = parseInt(readLine(), 10)
If someone could explain what this part of the code does. Does it introduce the function and variable and what is parse int? This loads on every code coach i do automatically and has comments telling me where to add my code so i know its necessary but not what for and how it affects the rest of the code.
0
switch(themeNumber)
{
case 1:
console.log("Light");
break;
// more cases follows here
}
parseInt() tries to convert the given input into an integer. If given input is somehow not convertible, then a NaN (Not a Number) was returned instead. This is what I've come to know about Javascript.
0
So do i get rid of parse int for the code to be read as a string or is there another way of doing that?
0
Try to read the input with just
var themeNumber = readLine();
And provide one of the string options (Light, Dark etc.) as input.
But then again, aestethically, the variable was named <themeNumber>, I would expect it to be created for storing a number rather than string : )
0
I had an extra space in one of my strings! Sometimes its the smallest details