New course! Every coder should learn Generative AI!
Try a free lesson+ 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 } }
16 Answers
+ 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())
+ 4
Gouhar Maqubool
JS course projects are running on Node JS so your code was right except 'light'
+ 3
It's a task?
+ 3
Thanks AJ i got the point where i done mistake there should "L" in uppercase 🙏
+ 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
+ 2
Gouhar Maqubool
You need to check spelling of 'light' in description
+ 2
Félix Zamdamela
Yes it is a task in JS course
+ 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
+ 2
It will also work but the main problem was i entered lowercase L in "light" thats why it showing not defined else
+ 1
It still showing refrenceError as promt is not define:(
+ 1
Gouhar Maqubool
prompt and alert is not a function of Node JS
use readLine() to read input values
+ 1
But it still showing wrong out as given the right input
+ 1
I hope this works
0
But you are saying that it is an error of prompt not defined
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] )
}
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)
}