0 of 5 problems solved0%
4 of 5

Move by Direction

Directions fit perfectly as a union of string literals. A switch on the direction narrows the union and makes it obvious how to adjust coordinates. Returning a new object keeps code predictable and avoids side effects.

type Step = "up" | "down" | "left" | "right";

Move by exactly one unit each time and keep the function pure.

Your Task

  • Create type Direction = 'up' | 'down' | 'left' | 'right'.
  • Create type Point = { x: number; y: number }.
  • Implement move(p: Point, dir: Direction): Point returning a new point moved by 1 in the given direction.