I’m at the “ES6: Use the Rest Operator with Function Parameters” of the JavaScript curriculum. I managed to get a result of 0 from the sum function when no arguments are passed on the call.
const sum = (...args) => {
if (args.length === 0) {
args.push(0);
}
return args.reduce((number, number2) => {
return number + number2;
});
}
console.log(sum(5, 2));
console.log(sum(5));
console.log(sum());
When I run the tests, I fail the last requirement which is " The sum
function uses the ...
spread operator on the args
parameter." as to say that in my code, the sum function doesn’t use the spread operator. I honestly have no idea what the problem is.
Could you help me?