1

I have this code here that I've set up:

var numbers = [1,2,3,4,5,6,7,8,9,2,3,4,5,6,1,2,3,4,5,6,7,8,8,4,3,2];
var greaterThan3 = numbers.filter (item => item >= 3);
console.log(greaterThan3);
greaterThan3.reduce((item, amount) => item + amount);

but it only gives me this:

[3, 4, 5, 6, 7, 8, 9, 3, 4, 5, 6, 3, 4, 5, 6, 7, 8, 8, 4, 3]

And, I know it probably has something to do with how I set up the .reduce() line. I'm trying to get the numbers greater than or equal to 3 to add up to one number. I'm assuming I have to define total and amount but I'm not sure. Just learning this chapter from Bloc so excuse the probably dumb question.

  • 1
    `reduce` returns the value.. So like: `var total = greaterThan3.reduce((item, amount) => item + amount);` – Eddie Jun 22 '18 at 16:39
  • Then `console.log` the total like: `console.log( total );` – Eddie Jun 22 '18 at 16:40
  • have you tried it on any browser console? it is already showing *108*. For your case try `sum = greaterThan3.reduce((item, amount) => item + amount); console.log(sum);` – A l w a y s S u n n y Jun 22 '18 at 16:40
  • .reduce returns a new value, so you should assign it properly: `greaterThan3 = greaterThan3.reduce((item, amount) => item + amount);` (either override the array, either resolve to another variable). Check the official docs for further information: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce – briosheje Jun 22 '18 at 16:41
  • 1
    @Eddie Awesome! It worked haha! Thank you! Slowly getting the hang of this. I appreciate it! – Emily Butterfield Jun 22 '18 at 16:42

0 Answers0