Implement a traffic-light UI with JS
SalesforceSoftware Engineer · Technical ScreenLast updated
This question evaluates front-end web development competencies, including DOM manipulation, CSS-driven UI state, JavaScript timing and asynchronous state management for interactive components.
Frontend Coding: Traffic Light
Build a simple traffic light component using HTML + CSS + JavaScript.
Requirements
- Render a vertical traffic light with three circles: red, yellow, green.
- Exactly one light is “on” at a time (brighter color); the others are “off” (dimmed).
- The light should cycle automatically in this order:
- Red for
Rmilliseconds - Green for
Gmilliseconds - Yellow for
Ymilliseconds - Repeat
- Provide a simple way to start the cycle when the page loads (e.g.,
window.onload).
Optional extensions (if time)
- Add Start / Pause / Reset controls.
- Allow changing
R/G/Ydurations without reloading. - Ensure timers don’t stack (no multiple overlapping intervals/timeouts).
Deliverable
- A single HTML page (or separate HTML/CSS/JS files) that demonstrates the cycling traffic light in a browser.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.