+ 1

Why does this code showing error?

The user can choose the color theme for the browser: 1. Light 2. Dark 3. Nocturne 4. Terminal 5. Indigo 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. Sample Input 2 Sample Output Dark function main() { var themeNumber = parseInt(readLine(2), 10) /* 1 - Light 2 - Dark 3 - Nocturne 4 - Terminal 5 - Indigo */ // Your code here switch (themeNumber){ case 1: console.log("light"); break case 2: console.log("Dark"); break case 3: console.log("Nocturne"); break case 4: console.log("Terminal"); break default: console.log("Indigo"); break } }

18th Oct 2022, 3:50 AM
Gouhar Maqubool
Gouhar Maqubool - avatar
16 Réponses
+ 4
Remove number 2 inside of readLine() function if you're trying debug you code on task problem, so if you trying to debug as a simple code remove readLine function your function should look like that 👇 var themeNumber = parseInt(prompt())
18th Oct 2022, 4:59 AM
Félix Zamdamela
Félix  Zamdamela - avatar
+ 4
Gouhar Maqubool JS course projects are running on Node JS so your code was right except 'light'
18th Oct 2022, 5:15 AM
A͢J
A͢J - avatar
+ 3
It's a task?
18th Oct 2022, 5:11 AM
Félix Zamdamela
Félix  Zamdamela - avatar
+ 3
Thanks AJ i got the point where i done mistake there should "L" in uppercase 🙏
18th Oct 2022, 5:14 AM
Gouhar Maqubool
Gouhar Maqubool - avatar
+ 2
There is no readLine() method in standard JavaScript (ECMAscript). If you actually read your error message, it tells you exactly this. If you want to run this in a browser, you can use the prompt() function to take user input. If you want to run it in node.js it is a bit more complicated, node does have a readline library (with lowercase L!!!) And you can see a pattern how to use it on Sololearn, here: https://www.sololearn.com/Discuss/2953345/?ref=app
18th Oct 2022, 4:50 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Gouhar Maqubool You need to check spelling of 'light' in description
18th Oct 2022, 5:11 AM
A͢J
A͢J - avatar
+ 2
Félix Zamdamela Yes it is a task in JS course
18th Oct 2022, 5:14 AM
A͢J
A͢J - avatar
+ 2
Its simple to do so You need prompt to define a number which is enclosed by parseInt()..like that var p; function main(){ p = prompt("Enter theme") var themeNumber = parseInt(p) } So you have to define a prompt like this Gouhar Maqubool
19th Oct 2022, 2:04 AM
^^^WILL_gamerz^^^
^^^WILL_gamerz^^^ - avatar
+ 2
It will also work but the main problem was i entered lowercase L in "light" thats why it showing not defined else
19th Oct 2022, 2:07 AM
Gouhar Maqubool
Gouhar Maqubool - avatar
+ 1
It still showing refrenceError as promt is not define:(
18th Oct 2022, 5:05 AM
Gouhar Maqubool
Gouhar Maqubool - avatar
+ 1
Gouhar Maqubool prompt and alert is not a function of Node JS use readLine() to read input values
18th Oct 2022, 5:07 AM
A͢J
A͢J - avatar
+ 1
But it still showing wrong out as given the right input
18th Oct 2022, 5:10 AM
Gouhar Maqubool
Gouhar Maqubool - avatar
+ 1
I hope this works
19th Oct 2022, 2:05 AM
^^^WILL_gamerz^^^
^^^WILL_gamerz^^^ - avatar
0
But you are saying that it is an error of prompt not defined
19th Oct 2022, 2:09 AM
^^^WILL_gamerz^^^
^^^WILL_gamerz^^^ - avatar
0
Code is right expect "light".you should use "Light". You can use a small code either. function main() { var themeNumber = parseInt(readLine(2) ,10); var themes = [ "Light", "Dark", "Nocturne", "Terminal", "Indigo" ] console.log(themes[themeNumber - 1] ) }
20th Oct 2022, 3:27 AM
Shenal Nimsara
Shenal Nimsara - avatar
0
Try this: function main() { var themeNumber = parseInt(readLine(), 10) switch(themeNumber ){ case 1: themeNumber = "Light"; break; case 2: themeNumber = "Dark"; break; case 3: themeNumber = "Nocturne"; break; case 4: themeNumber = "Terminal"; break; case 5: themeNumber = "Indigo"; } console.log(themeNumber) }
20th Oct 2022, 3:41 AM
Melisa Felder
Melisa Felder - avatar