r/learnjavascript • • 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");

}

}

}

1 Upvotes

3 comments sorted by

1

u/[deleted] Feb 07 '23

It wants a specific response for each code Green response: “Green light: Go!” Yellow response: can be anything that isn’t stop Red response: “Red light: you should stop.”

The code also needs to know what to do if the first letter of the color is capitalized or lowercase

You’ll need an or statement in your if

Ex: if(lightColor == “Green” || lightColor == “green”){

Hope I could help :)

-2

u/ForScale Jan 18 '23

Is this Java or JavaScript?

If it's Java, you want r/learnjava.