Switch code will not work. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 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"); }}

3rd Dec 2020, 6:23 AM
Emmanuel Riano
Emmanuel Riano - avatar
11 Answers
+ 6
Emmanuel Riano Any reason you are comparing string inside switch..🤔?
3rd Dec 2020, 6:40 AM
Piyush
Piyush - avatar
+ 1
You read <themeNumber> as an integer by parseInt(), but you compare it to string inside the switch.
3rd Dec 2020, 6:38 AM
Ipang
0
I honestly thought themenumber was the input expression that will compare the cases and give the output. what is wrong about it?
3rd Dec 2020, 7:18 PM
Emmanuel Riano
Emmanuel Riano - avatar
0
The problem here is, you can't compare numbers to strings, just like you can't compare apples to bananas.
3rd Dec 2020, 7:26 PM
Ipang
0
Right but whats the number in this situation ive used color names and closest thing to a number is the value themenumber
3rd Dec 2020, 7:29 PM
Emmanuel Riano
Emmanuel Riano - avatar
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?
3rd Dec 2020, 7:32 PM
Emmanuel Riano
Emmanuel Riano - avatar
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.
3rd Dec 2020, 7:38 PM
Emmanuel Riano
Emmanuel Riano - avatar
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.
3rd Dec 2020, 7:43 PM
Ipang
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?
3rd Dec 2020, 7:44 PM
Emmanuel Riano
Emmanuel Riano - avatar
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 : )
3rd Dec 2020, 7:48 PM
Ipang
0
I had an extra space in one of my strings! Sometimes its the smallest details
3rd Dec 2020, 8:43 PM
Emmanuel Riano
Emmanuel Riano - avatar