Back to js-conditionals
02-traffic-light.js
JavaScript
1/**
2 * 🚦 The Driving Simulator
3 *
4 * SafeDrive Driving School is building a simulator for new students.
5 * You need to write the logic that tells student drivers what to do
6 * when they encounter different traffic light signals.
7 *
8 * Signal → Action:
9 * - "green" → "GO"
10 * - "yellow" → "SLOW DOWN"
11 * - "red" → "STOP"
12 * - "flashing red" → "STOP AND PROCEED WITH CAUTION"
13 * - anything else → "INVALID SIGNAL"
14 *
15 * Rules:
16 * - The function should be case-insensitive
17 * (e.g., "GREEN", "Green", "green" should all return "GO")
18 *
19 * Hint: Use a switch statement!
20 *
21 * @param {string} color - The traffic light signal
22 * @returns {string} The driving action to take
23 */
24export function getTrafficAction(color) {
25 let data = color.toLowerCase()
26 switch(data){
27 case "green":
28 return "GO"
29 case "yellow":
30 return "SLOW DOWN"
31 case "red":
32 return "STOP"
33 case "flashing red":
34 return "STOP AND PROCEED WITH CAUTION"
35 default:
36 return "INVALID SIGNAL"
37 }
38}
39