Calculate the Total Marathon Length

Mary is excited to participate in a marathon, but when she signs up, she realizes that the total distance isn't mentioned in one clear number. Instead, the marathon length is broken down into smaller portions listed separately.

Mary needs to know the full distance she'll be running, so she asks you to help her by calculating the total length of the marathon.

Instructions:

  • Write a function named calculateMarathonLength that takes an array of numbers representing the different portions of the marathon distance.

  • The function should return the total distance of the marathon by summing up all the portions.

Note: Only use the map, filter, or reduce methods, or a combination of these methods, to solve the problem. Do not use loops to iterate or perform the operation.

Example 1

Input: calculateMarathonLength([3, 7, 8, 2, 5]);
Output: 25

Example 2

Input: calculateMarathonLength([7, 6, 8]);
Output: 21

Example 3

Input: calculateMarathonLength([10, 5]);
Output: 15

Are you stuck on this problem?

Get detailed, step-by-step solution and improve your JavaScript skills.

  • Detailed Explanation: Each solution comes with a comprehensive explanation, helping you understand the logic and concepts thoroughly.
  • Learn to Write Better Code: Even if you have written the code, discover ways to improve it and adopt best practices.
  • Save Time: Don’t waste hours struggling—get the solution and learn efficiently.
  • Lifetime Access: Get access to lifetime solutions for all problems available
Get the Solution

" I struggled with this problem for hours, but the solution provided here was clear and easy to follow. It helped me understand where I went wrong and improved my coding skills significantly." - Jane D.

If you're not satisfied, I offer a 7-day money-back guarantee.