# ES6 - Use the Rest Parameter with Function Parameters

**URL:** <https://forum.freecodecamp.org/t/es6-use-the-rest-parameter-with-function-parameters/568418>\
**Category:** I Built This\
**Created:** [November 2, 2022, 8:18pm UTC](https://forum.freecodecamp.org/t/es6-use-the-rest-parameter-with-function-parameters/568418 "2022-11-02T20:18:43Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![rafattack](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafattack/32/242003_2.png) [@rafattack](https://forum.freecodecamp.org/u/rafattack)\
**Post date:** [November 2, 2022, 8:18pm UTC](https://forum.freecodecamp.org/t/es6-use-the-rest-parameter-with-function-parameters/568418/1 "2022-11-02T20:18:43Z")

</div>

**Tell us what’s happening:**  
Describe your issue in detail here.

**Your code so far**

```javascript
const sum = (...args) => {
  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/106.0.0.0 Safari/537.36`

**Challenge:** ES6 - Use the Rest Parameter with Function Parameters

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-the-rest-parameter-with-function-parameters)**
>
> Learn to Code — For Free

so I spent 2 hours trying to figure this challenge out. then I gave up and checked the forum, then the Hint, then the solution. The code above is the solution given on the site, i did not write it.  
The forum and the Hint used functions that have not been taught yet on FreeCodeCamp. the reduce().

Did I skip some lessons?  
I completed every previous lesson twice (I like to finish a lesson than redo it a week later so i remember it better).

please help.

---

<div class="post-metadata">

**Author:** ![hbar1st](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hbar1st/32/372649_2.png) [@hbar1st](https://forum.freecodecamp.org/u/hbar1st)\
**Post date:** [November 2, 2022, 8:25pm UTC](https://forum.freecodecamp.org/t/es6-use-the-rest-parameter-with-function-parameters/568418/2 "2022-11-02T20:25:17Z")

</div>

i am unsure if I have understood the question. Do you mean you did not know about reduce? (or if not that one, which function did you have a question about?)

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [November 2, 2022, 8:29pm UTC](https://forum.freecodecamp.org/t/es6-use-the-rest-parameter-with-function-parameters/568418/3 "2022-11-02T20:29:21Z")

</div>

This is the starting code:

```javascript
const sum = (x, y, z) => {
  const args = [x, y, z];
  return args.reduce((a, b) => a + b, 0);
}

```

The reduce was already there. The challenge was asking you to change the first two line here.

---

<div class="post-metadata">

**Author:** ![rafattack](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafattack/32/242003_2.png) [@rafattack](https://forum.freecodecamp.org/u/rafattack)\
**Post date:** [November 2, 2022, 8:48pm UTC](https://forum.freecodecamp.org/t/es6-use-the-rest-parameter-with-function-parameters/568418/4 "2022-11-02T20:48:45Z")

</div>

I know, but I dont know what reduce means, so I cant modify a code without know what it means… i did look it up, but I want to make sure that im not missing anything.

---

<div class="post-metadata">

**Author:** ![rafattack](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafattack/32/242003_2.png) [@rafattack](https://forum.freecodecamp.org/u/rafattack)\
**Post date:** [November 2, 2022, 8:49pm UTC](https://forum.freecodecamp.org/t/es6-use-the-rest-parameter-with-function-parameters/568418/5 "2022-11-02T20:49:19Z")

</div>

yes, it is used in the example, but it was never taught.

---

<div class="post-metadata">

**Author:** ![rafattack](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafattack/32/242003_2.png) [@rafattack](https://forum.freecodecamp.org/u/rafattack)\
**Post date:** [November 2, 2022, 8:49pm UTC](https://forum.freecodecamp.org/t/es6-use-the-rest-parameter-with-function-parameters/568418/6 "2022-11-02T20:49:44Z")

</div>

and the solution\* used it as well

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [November 2, 2022, 8:50pm UTC](https://forum.freecodecamp.org/t/es6-use-the-rest-parameter-with-function-parameters/568418/7 "2022-11-02T20:50:40Z")

</div>

You shouldn’t modify the line with reduce at all. You were not asked to change that line. You should only change the function argument list.

---

<div class="post-metadata">

**Author:** ![hbar1st](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hbar1st/32/372649_2.png) [@hbar1st](https://forum.freecodecamp.org/u/hbar1st)\
**Post date:** [November 2, 2022, 8:51pm UTC](https://forum.freecodecamp.org/t/es6-use-the-rest-parameter-with-function-parameters/568418/8 "2022-11-02T20:51:34Z")

</div>

yes true. I understand that it definitely would look weird. I am glad you looked it up.  
I think it is a good instinct to look up any functions you do not understand before you modify the code.

---

<div class="post-metadata">

**Author:** ![jacques.nikola](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jacques.nikola/32/169946_2.png) [@jacques.nikola](https://forum.freecodecamp.org/u/jacques.nikola)\
**Post date:** [November 4, 2022, 1:37am UTC](https://forum.freecodecamp.org/t/es6-use-the-rest-parameter-with-function-parameters/568418/9 "2022-11-04T01:37:25Z")

</div>

If you write it this way it might be more clear:

`const cumulatedValue = array.reduce((previousValue, currentValue)=>previousValue+currentValue, 0);`

The reduce function returns a cumulated value that is the result of running the callback on each member of the array, starting with the start value (second argument of the reduce function). The reduce callback function takes two arguments, representing the previousValue and  
the currentValue.

For an array [1, 2, 3], here are the callback operations in order given the reduce function above:  
0 + 1 = 1  
1 + 2 = 3  
3 + 3 = 6

Each operation is `previousValue + currentValue`, as specified in the callback.

---

<div class="post-metadata">

**Author:** ![rafattack](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafattack/32/242003_2.png) [@rafattack](https://forum.freecodecamp.org/u/rafattack)\
**Post date:** [November 4, 2022, 3:59pm UTC](https://forum.freecodecamp.org/t/es6-use-the-rest-parameter-with-function-parameters/568418/10 "2022-11-04T15:59:00Z")

</div>

well actually no.  
usually they have commented lines telling you where or where not to change code. and in this lesson there were no lines.  
the instructions say:  
" Modify the function `sum` using the rest parameter in such a way that the function `sum` is able to take any number of arguments and return their sum."

and thats all besided the point of my question, which was: Did i miss any lessons on special functions.?

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [November 4, 2022, 4:04pm UTC](https://forum.freecodecamp.org/t/es6-use-the-rest-parameter-with-function-parameters/568418/11 "2022-11-04T16:04:40Z")

</div>

Well actually yes. You should not make any modification to the line with `reduce`. The instructions only talk about modifying the inputs.

The instructions say

> Modify the function `sum` using the rest parameter in such a way that the function `sum` is able to take any number of arguments and return their sum.

This is the original function:

```javascript
const sum = (x, y, z) => {
  const args = [x, y, z];
  return args.reduce((a, b) => a + b, 0);
}

```

Here is the example given for using a rest parameter:

```javascript
function howMany(...args) {
  return "You have passed " + args.length + " arguments.";
}
console.log(howMany(0, 1, 2));
console.log(howMany("string", null, [1, 2, 3], { }));

```

So, the “rest parameter” is the part `(...args)`. So, the solution needs that piece in the argument list:

```javascript
const sum = (...args) => {
  const args = [x, y, z];
  return args.reduce((a, b) => a + b, 0);
}

```

Oh, but now we have declared `args` twice, so we should fix that:

```javascript
const sum = (...args) => {
  return args.reduce((a, b) => a + b, 0);
}

```

Ah ha, the solution has been reached without doing a single thing to the line with `reduce`, consistent with the instructions.

Is it a bit weird that there is a function you haven’t seen before in the seed code? Sure. Do you need to do anything with it? No.

---

<div class="post-metadata">

**Author:** ![rafattack](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafattack/32/242003_2.png) [@rafattack](https://forum.freecodecamp.org/u/rafattack)\
**Post date:** [November 4, 2022, 4:14pm UTC](https://forum.freecodecamp.org/t/es6-use-the-rest-parameter-with-function-parameters/568418/12 "2022-11-04T16:14:24Z")

</div>

well, the thing is in order to know how to change the code in 1 function( Modify the function `sum`), i thought it would be necessary to know what the ‘return’ statement is saying, because it uses variables that aren’t anywhere else in the function a,b,c. otherwise its just guessing. I understand what your saying by the way, thank you.

---

<div class="post-metadata">

**Author:** ![hbar1st](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hbar1st/32/372649_2.png) [@hbar1st](https://forum.freecodecamp.org/u/hbar1st)\
**Post date:** [November 4, 2022, 4:16pm UTC](https://forum.freecodecamp.org/t/es6-use-the-rest-parameter-with-function-parameters/568418/13 "2022-11-04T16:16:49Z")

</div>

Just wanna say again. Your instincts (to look up what you don’t recognize) are not wrong. If you ever are asked to modify code and you do not understand it, definitely spend the time to do that first.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [May 6, 2023, 4:17am UTC](https://forum.freecodecamp.org/t/es6-use-the-rest-parameter-with-function-parameters/568418/14 "2023-05-06T04:17:22Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
