Here is the task:
Modify the function sum
using the rest parameter in such a way that the function sum
is able to take any number of arguments and return their sum.
Here is my code:
const sum = (function() {
return function sum(…args) {
return args.reduce((a, b) => a + b, 0);
};
})();
console.log(sum(0,1,2)); // 6
console.log(sum(1,2,3,4));
console.log(sum(5));
console.log(sum(0));
I cant pass the task because I have this message: The sum
function should use the ...
rest parameter on the args
parameter.
What shoud I do? How to pass it?
Your code so far
const sum = (function() {
return function sum(...args) {
return args.reduce((a, b) => a + b, 0);
};
})();
console.log(sum(0,1,2)); // 6
console.log(sum(1,2,3,4));
console.log(sum(5));
console.log(sum(0));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36
.
Challenge: Use the Rest Parameter with Function Parameters
Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-the-rest-parameter-with-function-parameters