Use the Rest Operator with Function Parameters problem with const args

Tell us what’s happening:
Hello, can you please tell me what I’m doing wrong with this code, when I try to run the test nothing happens. Is there a problem with const args ?
Thank you

Your code so far

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

Your browser information:

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

Link to the challenge:

const args = [ x, y, z ] delete this, you’re misunderstanding how the rest operator works. function sum(...args) - inside that function, args is an array of the arguments, you don’t need to redefine it, you’ve made it available by using ...args

1 Like

Thank you for explaining ! I think I need to read up more on the rest operator .

In this case it’s a way to be explicit about what your intentions are. You could just have

function sum() {
  const args = Array.from(args);
  return args.reduce...

So sum takes any number of arguments, and then you convert those arguments to an array, then sum using reduce.

But you can be more explicit, and say up front that the function takes any number of arguments, and get rid of that conversion step - makes it much clearer to readers of the code (both people and to the JavaScript engine that needs to compile and execute the code) what is going on

function sum(...args) {
  return args.reduce...

ok , I get it , thank you again.

1 Like