Traversing a Path in a 2D Array

You are given a two dimensional array containing arrays of integers filled with 0's and 1's as an argument. The 1's represent a path that can be traversed from the top left (0 index of the first inner array) to the bottom right (last index of the last inner array). Return a series of moves represented as an array of strings 'left', 'right', 'up', down' needed to get from the top left to the bottom right.

The first inner array will always have a 1 in its 0 index.

There will also always be a path guaranteed to reach the bottom right from the top left.


  • Must return an array of strings

Example #1

  [1, 1, 0, 0],
  [0, 1, 0, 0],
  [0, 1, 0, 0],
  [0, 1, 1, 1]
> ['right', 'down', 'down', 'down', 'right', 'right]

  1. Starting at the top left 1, we move one step to the right, to the next 1 value.
  2. We then take three down steps.
  3. We then take two steps to the right and reach the destination.
  4. We return an array containing those steps: 'right', 'down', 'down', 'down', 'right', 'right.