Es6-sum-numbers

how to solve the test below?

You need to define a function which will add numbers and return the sum. Use rest operator feature of ES6+ to accept numbers from the user and calculate its sum. Example: sum(1) //1 sum(1,2) // 3 sum(1,2,4) // 7 sum(1,2,3,4,5,6,7) // 28

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

This challenge has some information (and a hint page if needed)

I’m new to this and I can’t understand the exercise, can you help me?

What don’t you understand about the challenge? I can see the picture. No need to post it again. I cannot see into your brain to know what parts are confusing for you.

I’m new to this and I can’t understand this exercise, I literally don’t know how to perform it.

What words or phrases are confusing you?

We aren’t going to write the answer for you, so we need to know what you need explained?

I could just start by assuming you know zero JavaScript, but that would be unproductive.

I don’t want you to write to me, but to guide me on how to do it, I’ll be grateful

Do you know what a function is?

Do you know how to add numbers in JavaScript?

Do you know how to take different numbers of arguments for a JavaScript function?

Do you know what a rest parameter is?

I’m a new student of Javascript, I learned the first two questions, the last two I didn’t.

Ok, start with what you know.

Suppose your function takes in an array of numbers. How do you get the sum of that array?

Online course school - Alura Courses and courses on platform videos.

I would assume you have learned about rest parameters before being given a challenge that involves using it. If not you are either skipping something or the curriculum isn’t very good.


Read the MDN page on rest parameters.

If you want to use reduce for the addition check the reduce page as well.

Both have examples of doing this exact thing.

As a solution I managed to solve it as follows.

function sum(...theArgs) {
  let total = 0;
  for (const arg of theArgs) {
    total += arg;
  }
  return total;
}

console.log(sum(1, 2, 3));
// expected output - 6

console.log(sum(1, 2, 3, 4));
// expected output - 10

After studying a lot, I came up with a solution phew! It was the only one I needed, thanks a lot for the previous help

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