Last active
August 23, 2018 17:47
-
-
Save mojaray2k/057bcde7b8e4d889fdc500826d334afb to your computer and use it in GitHub Desktop.
The Reduce Array Method Example 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Distance Traveled | |
* Use the 'reduce' helper to find the sum of all the distances traveled. | |
* Assign the result to the variable 'totalDistance' | |
*/ | |
const trips = [{ distance: 34 }, { distance: 12 } , { distance: 1 }]; | |
const totalDistance = trips.reduce((total, trip) => { | |
total += trip.distance | |
return total; | |
}, 0); | |
console.log(totalDistance); | |
/ Result is: 47 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment