Rest operator challenge problem

Rest operator challenge problem


I’m confused. The one test that I cannot get it to pass is:

“The sum function uses the … spread operator on the args parameter.”

I find this confusing. I’ve used the spread operator in both the arg variable and the arguments for the return statement. I have tried putting it other places in the code, but that seems to ruin the whole thing when I do that.

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

Problem Use the Rest Operator with Function Parameters

In the future, please create your own topic when you have specific questions about your own challenge code? Only respond to another thread when you want to provide help to the original poster of the other thread.

The easiest way to create a topic for help with your own solution is to click the Ask for Help button located on each challenge. This will automatically import your code in a readable format and pull in the challenge url while still allowing you to ask any question about the challenge or your code.

Thank you.

The challenge you are working on asks you to modify the function sum so that it uses the rest operator and it works in the same way with any number of parameters.

The rest operator is ONLY used in a function’s parameter section. You should not be writing the following, because then you are using the spread operator, which is completely different.

const args = [ ...z ]

If you look at the remainder of the function, there is an args variable which is an array. The rest operator allows you to create an array of the functions arguments. So, you need to make sure you name the variable which comes after the ... the same name as the array in which the reduce method is being called on.


Thank you! Now, I understand.


This does clarify the task, but that only goes to show that the task needs adjustment. Line 4 should be removed entirely. It only obfuscates the code changes needed.


Still confused. So what is the final code?

Why does this not work?

Help much appreciated

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


Don’t worry I figured it out :slight_smile: