← Back to challenges

Make My Way Home

JavaScriptHardnumbersmath

Instructions

You will be given a list, showing how far James travels away from his home for each day. He may choose to travel towards or away from his house, so negative values are to be expected.

Create a function that calculates what distance James must walk to get back home.

Examples

distanceHome([2, 4, 2, 5]) ➞ 13

distanceHome([-1, -4, -3, -2]) ➞ 10

distanceHome([3, 4, -5, -2]) ➞ 0

Notes

  • Assume that James only travels in a straight line.
  • Distance is always a positive number.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Add a Consecutive List of Numbers