Make My Way Home

Published by Mubashir Hassan in

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.
Watch a quick demo on how Edabit works.