While this seems OK, in reality it has a nefarious bug! Open your console and type:
This returns a segment object, which looks great! But what happens if we try
a index of 0?
Instead of a segment, we get undefined! This means we can't get a
direction! Let's use an if statement to return the segment at the index if
there isn't a segment further forward. Change segmentFurtherForwardThan to:
Now when you run segmentFurtherForwardThan(0, snake) it will return the
snakes head instead of undefined. Excellent! Now we get to change moveSnake to
use segmentFurtherForwardThan when it assigns the segments direction. Change moveSnake to:
array.map and array.forEach pass 3 arguments to the function it is given: the
first is the element in the collection, the second is its index (or location in the collection) and
the third is the collection itself.