ES6: Use the Rest Operator with Function Parameters not working

Hi,

My code works only in these two: " The result of sum(0,1,2) should be 3" and " he result of sum(1,2,3,4) should be 10".

Everything else is wrong, I mean these parts:
" The result of sum() should be 0".
" The result of sum(5) should be 5".
" The sum function uses the ... spread operator on the args parameter.".
It also says:“TypeError: unknown: Duplicate declaration “args””.

How could I improve this and what is wrong?:

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

you should have only one parameter in your function, so to avoid needing the line in which you redefine args

1 Like

Only one parameter? Like a? Nothing seems to work.

like args

this below is because you can’t redeclare variables

1 Like