intermediate20 min

Direction Sense

Track position after a sequence of cardinal moves and find the straight-line distance.

What you'll learn

  • Track a position as (x, y) coordinates after a sequence of North/South/East/West moves
  • Compute the straight-line distance from the starting point using the Pythagorean theorem
  • Determine facing direction after a 90 or 180 degree turn

Prerequisites

Explanation

Direction sense questions describe someone walking in a sequence of directions and ask either their final facing direction or their straight-line distance from the start. The reliable method is to track position as coordinates, not to try to picture the whole path in your head.

Treat North as +y, South as -y, East as +x, West as -x, starting at (0, 0). Walking 4 km north then 3 km east moves you to (3, 4) -- 3 east, 4 north. The straight-line distance from the start is not the total distance walked (7 km); it's the direct distance, found with the Pythagorean theorem: sqrt(x^2 + y^2) = sqrt(3^2 + 4^2) = sqrt(25) = 5 km. This "3-4-5 triangle" pattern shows up constantly in direction problems because the numbers are chosen to come out clean.

Turns change which direction "forward" means, without necessarily changing position by themselves. Facing East and turning 180 degrees puts you facing West. Facing North and turning right (clockwise) puts you facing East. The trap is tracking turns cumulatively in your head across a long sequence -- it's far more reliable to update a single "current facing" variable one turn at a time than to try to reason about the net effect of several turns at once.

Two moves in opposite directions cancel out. Walking 5 km north then 5 km south returns you to a net y-displacement of 0 -- you're back where you started on that axis, even though you walked 10 km in total. Always compute net displacement per axis before reaching for the distance formula.

Coordinate tracking

Start at (0,0). North increases y, South decreases y, East increases x, West decreases x. Straight-line distance from the start is sqrt(x^2 + y^2), regardless of the path taken to get there.

Example

Tracking net position after a sequence of cardinal-direction moves.

function finalPosition(moves) {
  let x = 0, y = 0;
  for (const m of moves) {
    if (m.direction === 'N') y += m.distance;
    else if (m.direction === 'S') y -= m.distance;
    else if (m.direction === 'E') x += m.distance;
    else if (m.direction === 'W') x -= m.distance;
  }
  return { x, y };
}
// finalPosition([{ direction: 'N', distance: 4 }, { direction: 'E', distance: 3 }]) -> { x: 3, y: 4 }

Guided exercise

Guided exercise

Write finalPosition(moves) where moves is an array of { direction: 'N'|'S'|'E'|'W', distance }. Track and return the final { x, y } position starting from (0, 0).

Checks: Tracks a simple two-move path · Nets out two moves on the same axis · plus 1 hidden check

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Stuck? Get a hint.

Independent exercise

Independent exercise

Write straightLineDistanceFromStart(moves) that reuses the provided finalPosition helper to find the net (x, y), then returns the straight-line distance from the origin, rounded to 2 decimal places.

Checks: Computes a classic 3-4-5 triangle distance · Returns 0 when moves fully cancel out · plus 1 hidden check

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Stuck? Get a hint.

Common mistakes

  • Confusing total distance walked with straight-line distance from the starting point.
  • Tracking cumulative turns mentally across a long sequence instead of one step at a time.
  • Forgetting that opposite-direction moves on the same axis partially or fully cancel out.

Knowledge check

Knowledge check

1. Starting at the origin facing North, a person walks 4 km, turns right (now facing East), and walks 3 km. How far is the person from the start (straight-line distance)?
2. If you are facing East and turn 180 degrees, which direction do you now face?
3. What is the safest way to handle a long sequence of turns in a direction-sense question?

Takeaway

Track net (x, y) position per axis rather than trying to picture the whole path -- the Pythagorean theorem does the rest.

Summary

Direction-sense problems are solved by treating each cardinal move as a coordinate change, netting out moves on the same axis, and applying the Pythagorean theorem for straight-line distance.

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.