Coding task · JavaScript and React course · Module 15: Patterns and Architecture

Practice: Docking State Machine

A state machine makes sure the process is always in exactly one of the defined states: a transition the map does not allow simply changes nothing. Optional chaining ?. returns undefined instead of throwing when the state is missing from the map, and ?? substitutes a fallback value for undefined. Boolean(...) turns the found string or undefined into true or false. Object.keys returns the field names of an object, here the names of the events available in a state.

Task

Finish the state machine of the docking process. ___BLANK1___: after the UNDOCK event the ship goes back to the approaching state. ___BLANK2___: send returns the next state from the map, machine[current]?.[event], and when the current state has no transition for this event, it stays in current. ___BLANK3___: canSend(event) returns true or false depending on whether the transition exists in the current state. ___BLANK4___: events are the event names of the transition object of the current state (for a state missing from the map, an empty object, so an empty list). DockingControl creates buttons from events.