Hone logo
Hone
Problems

Building a Simple Traffic Light State Machine in JavaScript

State machines are a fundamental concept in computer science, used to model systems that can exist in one of a finite number of states. This challenge will guide you through creating a basic traffic light state machine in JavaScript, a common and practical application of this pattern. Understanding state machines will enhance your ability to design robust and predictable software.

Problem Description

Your task is to implement a JavaScript function or class that simulates a traffic light. The traffic light has three distinct states: "RED", "YELLOW", and "GREEN". The light transitions between these states in a specific sequence: RED -> GREEN -> YELLOW -> RED.

Your implementation should:

  • Maintain the current state: The state machine must be able to report its current state.
  • Transition to the next state: Provide a mechanism to trigger a transition to the next state in the defined sequence.
  • Handle invalid transitions (optional but recommended): While not strictly required for this basic example, consider how you might handle attempts to transition to an invalid state or from an invalid current state. For this challenge, we'll assume valid initial states and transitions.

The expected behavior is that when the transition function is called, the light moves from its current state to the next in the sequence. For example, if the light is currently "RED", calling the transition function should change its state to "GREEN".

Examples

Example 1:

// Assume a StateMachine class is implemented
const trafficLight = new StateMachine("RED"); // Initial state

console.log(trafficLight.getCurrentState()); // Output: "RED"

trafficLight.transition();
console.log(trafficLight.getCurrentState()); // Output: "GREEN"

trafficLight.transition();
console.log(trafficLight.getCurrentState()); // Output: "YELLOW"

trafficLight.transition();
console.log(trafficLight.getCurrentState()); // Output: "RED"

Explanation: The traffic light is initialized to "RED". Each subsequent call to transition() moves it to the next state in the RED -> GREEN -> YELLOW -> RED cycle.

Example 2:

const trafficLight = new StateMachine("GREEN");

console.log(trafficLight.getCurrentState()); // Output: "GREEN"

trafficLight.transition();
console.log(trafficLight.getCurrentState()); // Output: "YELLOW"

Explanation: Starting from "GREEN", one transition() call correctly moves the light to "YELLOW".

Constraints

  • The state machine must support exactly three states: "RED", "YELLOW", and "GREEN".
  • The transition sequence is fixed: RED -> GREEN -> YELLOW -> RED.
  • The initial state can be any of the three defined states.
  • The implementation should be in JavaScript.

Notes

Consider using a JavaScript class or a factory function to encapsulate the state machine's logic. You'll need to manage the current state internally and define a clear mechanism for transitioning between states. Think about how you can represent the sequence of states efficiently.

Loading editor...
javascript