Skip to content

Commit

Permalink
Exercise 01 - Creating a State Machine - switch
Browse files Browse the repository at this point in the history
  • Loading branch information
chesterheng committed May 14, 2020
1 parent 9883a4b commit dc4fd0a
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions 01/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,38 @@ const elBox = document.querySelector('#box');

// Pure function that returns the next state,
// given the current state and sent event
function transition(state, event) {
switch (
state
// Add your state/event transitions here
// to determine and return the next state
) {
const transition = (state, event) => {
switch (state) {
case "inactive":
switch (event) {
case "click":
return "active";
default:
return state;
}
case "active":
switch (event) {
case "click":
return "inactive";
default:
return state;
}
default:
return state;
}
}

// Keep track of your current state
let currentState = undefined;
// start with inactive initial state
let currentState = "inactive";

function send(event) {
const send = event => {
// Determine the next value of `currentState`

currentState = transition(currentState, event)
elBox.dataset.state = currentState;
}

elBox.addEventListener('click', () => {
// send a click event
send("click")
});

0 comments on commit dc4fd0a

Please sign in to comment.