I need to know about this Q&A

Tell us what’s happening:

Your code so far


const sum = (x, y, z) => {
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/77.0.3865.120 Safari/537.36.

Challenge: Use the Rest Parameter with Function Parameters

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-the-rest-parameter-with-function-parameters

well if you want to know more about it try the bellow link

also the example gives out a function

function howMany(...args) {

don’t u think using a function instead of

const sum = (x, y, z) => {

can help you out here?

I hate when FreecodeCamp introduces new functions without even explaining them… directly from the problem in questions

The rest parameter eliminates the need to check the args array and allows us to apply map() , filter() and reduce() on the parameters array.

Yet map(),filter() and reduce() have not been taught… how is someone expected to use these, if these are not even used in the example?

4 Likes

You don’t have to make any changes to this line in the starter code:

return args.reduce((a, b) => a + b, 0);

Knowledge of how to use reduce to sum up an array of numbers is not needed to solve the problem.

2 Likes

[Spoiler- Dont read on if you are still working on the problem]

@colinthornton Yes, I understand that - but if you look at the number of people who have asked questions a lot are getting caught up with the fact of a new function.

all we have to do is undeclare const args since its declared by the spread out ( method? ) …args - I took a minute to realize that reduce() is not needed… Im sure a lot of greenhorns see that and freak a bit, while Im sure a lot see it and dont flinch since they know what the quesiton is asking.

I guess it would have been easier if they just used something else for the return that we had worked on, as to not get ourselves worked up on something that was not needed.

2 Likes

Yes. It should be introduced clearly before.

3 Likes

I know it’s been a while since this was asked, but it’s worth pointing out (and it’s been my experience with previous lessons as well) that this is much easier than what it looks like.
Since it contains some concepts we haven’t seen previously (“reduce”), and the new rest parameter is only explained briefly (appreciate in a short lesson you can’t go too much into detail), it’s easy to get stuck.

I passed it by using:

const sum = (...args) => {

  return args.reduce((a, b) => a + b, 0);

}

console.log(sum(1,2,3,4))

You basically change the function argument to use the rest parameter ("…name") and delete the second line as you don’t need it at all. You don’t need to touch the “reduce” part. Use console.log to test it out.

Lastly, it’s always good practice to develop the habit of also looking things up on our own and figure them out - it’s a lot of what we’re going to do as developers anyway.

1 Like

@jagcb @chrismdbb @thang.phamccie

I too was frustrated with not knowing what the .reduce method was doing…
I found this article helpful in understanding that

1 Like

I quit trying to be practical. Here is my caveman solution:

const sum = (…args) => {
var summ = 0
for(let i = 0; i < args.length; i++){
summ = summ + args[i];
}
return summ ;
}

Tossing my hat into the ring here. No explanation of the .reduce() method now has me looking up exactly what the .reduce() method does.

I understand we should be looking up things that we do not know, however, in order for a learner to logically understand what’s going on here, it seems like a mandatory pre-req to understand the .reduce() method.

The problem itself should alert the learner that this understanding is needed to follow the logic.

  • Edit - I still don’t understand this problem.
1 Like