Skip to content

Instantly share code, notes, and snippets.

@MicFin
Created May 8, 2017 18:44
Show Gist options
  • Save MicFin/b7bf8046ea6e4b5bbe44ef5da2bc32e4 to your computer and use it in GitHub Desktop.
Save MicFin/b7bf8046ea6e4b5bbe44ef5da2bc32e4 to your computer and use it in GitHub Desktop.
'use strict'
// Example array
const numsArray = [0, 1, 2, 3, 4]
// Find the sum of all elements in an array using anonymous arrow function
const sum = numsArray.reduce((prev, curr) => prev + curr)
// Find the sum of all elements in an array using named arrow function
const add = (prev, curr) => {
return prev + curr
}
sum = numsArray.reduce(add)
// Find the largest of all elements in an array using anonymous arrow function
let largest = numsArray.reduce((prev, curr) => prev > curr ? prev : curr)
// Find the largest of all elements in an array using named arrow function
const max = (prev, curr) => {
return prev > curr ? prev : curr
}
largest = numsArray.reduce(max)
// Find the total number of rockets launched in an array
// using anonymous arrow function
// start the count with 0
const rockets = [
{ country: 'Russia', launches: 32 },
{ country: 'US', launches: 23 },
{ country: 'China', launches: 16 },
{ country: 'Europe(ESA)', launches: 7 },
{ country: 'India', launches: 4 },
{ country: 'Japan', launches: 3 }
]
rockets.reduce((prev, curr) => {
return prev + curr.launches
}, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment