From 9adcb1575e195813a640e55bad5996d05c64d5a4 Mon Sep 17 00:00:00 2001 From: Derrek Gass Date: Tue, 13 Oct 2020 14:18:11 -0700 Subject: [PATCH] map filter reduce --- solutions/day-01/functional.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/solutions/day-01/functional.js b/solutions/day-01/functional.js index 611b0c2..434cdeb 100644 --- a/solutions/day-01/functional.js +++ b/solutions/day-01/functional.js @@ -36,4 +36,21 @@ const newNumbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] const evens = newNumbers.filter((n) => n % 2 === 0) const odds = newNumbers.filter((n) => n % 2 !== 0) console.log(odds) -console.log(evens) \ No newline at end of file +console.log(evens) + +// You can associate reduce with a blender. You put different fruits to a blend and you get a mix of fruit juice. +// The juice is the output from the reduction process. +// We use the reduce method to sum all numbers in an array together, or +// to multiply items in an array or to concatenate items in an array. + +const sumReduce = numbers.reduce((acc, cur) => acc + cur, 5) +console.log(sumReduce) + +const strs = ['Hello', 'World', '!'] +const helloWorldReduce = strs.reduce((acc, cur) => acc + ' ' + cur) +console.log(helloWorldReduce) + +// multReduce has an initial value of 1 (default param), then multiplying the current array value against the accumulated value +const multReduce = odds.reduce((acc, cur) => acc * cur, 1) +console.log(multReduce) +