r/learnjavascript • u/Common-Passenger5222 • Jan 18 '23
CodeHS - 2.9.8 Stop Light
Hey, I need some help with my javascript hw. Here it is:
Write a program that prompts the user for a color: red, yellow, or green (the colors of a stoplight). Then using if statements and else statements, print the user a message describing what they should do. For example if the color was red, you could say something like, “Red light: you should stop.”
As a bonus, see if you can use “else if” in this problem. Check out “else if” in the “Docs” tab.
You may assume the user will only input “red”, “yellow”, or “green”. You do not have to worry about other colors, or mixed capitalization such as “blue” or “ReD”, but you can make your program handle these cases if you wish!
Heres what I have now:
function start(){
var lightColor = readLine("What color is the light? ");
if (lightColor = "green"){
println("Go");
}
else{
if (lightColor = "red"){
println("Stop");
}
}
else{
if (lightColor = "yellow"){
println("Slow");
}
}
}