You can also code a Finite State Machine (FSM) in JavaScript (if you understand the language).
Here's a link to a tutorial I wrote for my IoT students to code a FSM for UK traffic lights.
There's a link to a NR flow as well.
Note: If you look at the NR flow you will see the classic way of coding a FSM using 'case' statements.
This traffic light example just moves from one state to another every 3-secs (so you can just use a simple counter in this very basic example).