Traffic Light
Programming Basics · JavaScript
TASK
Problem
Given traffic-light color `red`, `yellow`, or `green`, print `STOP`, `WAIT`, or `GO`.
EXAMPLE
Example
green
GO
LIMITS
Constraints
color ∈ {red, yellow, green}
LEARN
Theory for this problem
+
### Traffic Light
JavaScript arithmetic works with numeric values stored in variables. A clear solution reads the needed values, computes the formula with `+`, `-`, `*`, `/` or `**`, and prints only the final result.
NEED HELP?
Hints
+
['Write the formula using named intermediate values if it contains more than one operation.', 'Before printing, output only the computed result and do not add explanatory text that is absent from the required format.']
ANSWER
Solution
+
const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trimEnd();
let c = input;
if ((c) === ("red")) {
console.log("STOP");
} else {
if ((c) === ("yellow")) {
console.log("WAIT");
} else {
console.log("GO");
}
}