Switch Case question - Code error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Switch Case question - Code error

Hey, I just tried to solve a sololearn question but it won't work. I need to to several cases for giving out some text. If I don't change the first sentence given by sololearn (you can see them I commented it) then I get a massive error Message, so I changed it to just readline (). Now I got the problem, that It show no output. So for example if sololearn types in 1 it shows no output. Could you explain to me my error? function main() { var themeNumber = readLine() // var themeNumber = parseInt(readLine(), 10) /* 1 - Light 2 - Dark 3 - Nocturne 4 - Terminal 5 - Indigo */ // Your code here switch (themeNumber) { case 1: document.write("Light"); break; case 2: document.write("Dark"); break; case 3: document.write("Nocturne"); break; case 4: document.write("Terminal"); break; case 5: document.write("Indigo"); } }

8th Sep 2021, 3:05 PM
Deniz Lier
Deniz Lier - avatar
17 Answers
+ 4
Dennis Lier , try to change everywhere document.write with console.log.
8th Sep 2021, 3:08 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 4
var themeNumber = parseInt(readLine(), 10) ✔️ var themeNumber =readLine () //var themeNumber= parseInt(readLine(),10)❌ // is used for comment and the code after // will be ignored. According to the people above said ,Use console.log instead of document.write b.coz we wanna show o/p in console not in web
8th Sep 2021, 4:18 PM
Myo Thuzar
Myo Thuzar - avatar
+ 3
readLine() returns string by default. You can use parseInt(readLine(), 10) to convert it to an integer. Or you can change your cases to strings case "1": case "2": ...
8th Sep 2021, 3:33 PM
Simba
Simba - avatar
+ 1
I wonder if it's the missing break that causes the error in case 5
8th Sep 2021, 3:16 PM
Tim
Tim - avatar
+ 1
TheWh¡teCat 🇧🇬 and Nick thank you for the fast answer, both options don't work here for me. Guess something different is wrong. It show absolutely no error in the console, just no output
8th Sep 2021, 3:22 PM
Deniz Lier
Deniz Lier - avatar
+ 1
TheWh¡teCat 🇧🇬 as mentioned I commented the second line because it gave me an error in the console, so I tried the option for strings
8th Sep 2021, 7:22 PM
Deniz Lier
Deniz Lier - avatar
+ 1
Myo Thuzar I commented the second line on purpose, cause it caused an error in the console
8th Sep 2021, 7:23 PM
Deniz Lier
Deniz Lier - avatar
+ 1
Myo Thuzar TheWh¡teCat 🇧🇬 Simba function main() { //var themeNumber = readLine() var themeNumber = parseInt(readLine(), 10) /* 1 - Light 2 - Dark 3 - Nocturne 4 - Terminal 5 - Indigo */ // Your code here switch (themeNumber) { case 1: document.write("Light"); break; case 2: document.write("Dark"); break; case 3: document.write("Nocturne"); break; case 4: document.write("Terminal"); break; case 5: document.write("Indigo"); } } This is the new code, where I uncommented the integer line. Now the problem is, that the console tells me that "document" in the first case from "light" is not defined
8th Sep 2021, 7:26 PM
Deniz Lier
Deniz Lier - avatar
+ 1
Dennis Lier , replace it with console.log as I mentioned previously.
8th Sep 2021, 7:28 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
Dennis Lier Did you call the function main()?
8th Sep 2021, 3:24 PM
Tim
Tim - avatar
0
Dennis Lier , then read themeNumber as integer (uncomment the second line in the function and remove the first line).
8th Sep 2021, 3:31 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
TheWh¡teCat 🇧🇬 it worked just fine lol.. could you explain to me why document.write led to a error in this case? And thank you so much!
8th Sep 2021, 7:30 PM
Deniz Lier
Deniz Lier - avatar
8th Sep 2021, 7:32 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
TheWh¡teCat 🇧🇬 I'm really new in learning JavaScript I don't really know what function main is
9th Sep 2021, 3:04 PM
Deniz Lier
Deniz Lier - avatar
0
Nice
10th Sep 2021, 10:29 AM
Sabari Siva Prakash
Sabari Siva Prakash - avatar
0
i did have the same problem why sololearn teach us in the leson with document.write and in the problems the expect us to use console.log when they dint teach us that?
23rd Sep 2021, 7:39 PM
Fernando Enrique Ametller Medina
Fernando Enrique Ametller Medina - avatar
0
Fernando Enrique Ametller Medina This was exactly my question. Sometimes it's just not understandable what they want and how they check
26th Sep 2021, 9:59 PM
Deniz Lier
Deniz Lier - avatar