Rest Operator doesn't works

Rest Operator doesn't works
0.0 0

#1

Tell us what’s happening:

I set the rest operator inside the function but it doesn’t show what expect

Your code so far


const sum = (function() {
  "use strict";
  return function sum(...theArgs) {
    
    return theArgs.reduce((a, b) => a + b, 1);
  };
})();
console.log(sum(1, 2, 3)); // 6

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-the-rest-operator-with-function-parameters


#2

Hi @lisarko8077

I think the issue is that you’re providing 1 as the second argument to the reduce function, which would become the first value to appear as a, see here if you’re confused. So effectively, every time you call sum, you’re adding an extra 1 to every calculation.

I suspect you probably wanted to do this (and this is also whats provided by default):

return theArgs.reduce((a, b) => a + b, 0);

#3

I tried your change but it return all tests ok except of the last where it return the message “The sum function uses the … spread operator on the args parameter.”


#4

It seems the tests are being a little bit picky, even though your solution is perfectly valid. You need to change the argument from ...theArgs to ...args and the call to reduce to match.


#5

Your solution was good , just i forgot to use the map function to square numbers