Read here. Code on desktop. The coding workspace opens on screens 1024px and wider.
JavaScript interview question
Turtle
medium20 min
Move a turtle around a two-dimensional plane using forward/backward steps and quarter-turn commands.
Requirements
Export a Turtle class as the default export. new Turtle() starts at {x:0,y:0}, facing east. Positive x points east; positive y points north. This is a mathematical plane, not screen coordinates.
forward(distance = 1) moves along the current direction. backward(distance = 1) moves in the opposite direction without changing heading. Assume finite numeric distances; zero, fractional, and negative distances are valid.
left() turns 90 degrees counterclockwise; right() turns 90 degrees clockwise. Turning never changes position. Four turns in the same direction restore the original heading.
All four movement/turn methods return the same instance for chaining. getPosition() returns a fresh {x,y} snapshot; modifying that snapshot must not move the turtle.
getDirection() returns east, north, west, or south. Instances have independent state. No DOM, canvas rendering, HTML, or CSS is required.
Examples
Example 1
Input: new Turtle().forward(3).left().forward(2).getPosition()
Output: {x:3,y:2}
Example 2
Input: new Turtle().right().forward(4).getDirection()
Output: "south"
A stateful Turtle class with cardinal headings and chainable commands.