Tell us what’s happening:
Your code so far
const sum = ((...args) =>{
"use strict";
return args.reduce((a,b)=> a+b)
})();
console.log(sum(0,1,2)); // 6
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-the-rest-operator-with-function-parameters/
Hi,
You are using rest operator in the wrong place. The returned function was originally written to take three parameters. The challenge is for you make a new function that will take an undetermined number of parameters
const sum = (function() {
"use strict";
return function sum(x, y, z) { // <- replace this parameter list with rest operator
const args = [ x, y, z ];
return args.reduce((a, b) => a + b, 0);
};
})();
console.log(sum(1, 2, 3)); // 6
This challenge is a bit confusing because the function is wrapped in an IIFE. Not sure why - maybe it eliminated possible wrong answers that would still pass the test. Just concern yourself with this part of the code
function sum(x, y, z) {
const args = [ x, y, z ];
return args.reduce((a, b) => a + b, 0);
};
Good luck