repl.it
@ManavMisra/

Array Reduce Demo

Nodejs

No description

fork
loading
Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const myArray = [1, 2, 3, 4, 5];

// Add up all of the numbers and get the total.
const sum = myArray.reduce((total, num) => total + num);

const oddSum = myArray.filter(num => num % 2 === 1).map(num => num * 2).reduce((total, num) => total + num);

// const sum = myArray.reduce((accumulator, current) => {
//   return accumulator + current;
// })

console.log(oddSum);

// Write a function called sumArray that takes an array of numbers and returns the sum of all of those numbers added together.

// function sumArray(arr) {
//   // return the result of running reduce on the arr that got passed in.
// }

// const sum = sumArray([1, 2, 3, 4, 5])
?