1

I need to create a function, that will parse function parameter of integer to an array list.

So if we call the function like this: sum(1, 4 ,7); then inside of the function 'sum' the arguments variable/keyword will look like this [1, 4, 7]. I need to create a sum function so that it can take any number of arguments and return the sum of all of them.

Here's my code so far:

function sum ([a,b]) {
var arr = [a,b];
var sum =arr.reduce(add, 0);

function add(a, b) {
return a + b;
}
}

I also was trying to so something like this:

function sum ({
  arr[0]: 1;
  arr[1]: 2;
  arr[2]:3;

  var sum =arr.reduce(add, 0);

function add(a, b) {
return a + b;
}
})

but I obviously doing something wrong.

4 Answers 4

2

Try this

function sun () {
  return Array.prototype.reduce.call(arguments, function(pre, cur) {
    return pre + cur
  }, 0)
}

This is your need.

Sign up to request clarification or add additional context in comments.

7 Comments

Explain what you've done. Code only answers are not useful. Also, you've got typos. ALSO, you've baked "add" into this function, which is not what OP requested.
Thanks, but what is "pre" and "cur" in here? Just an elements of the array?
@AliceJarmusch this is two of the params of Array.prototype.reduce developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
@cale_b My mistake @AliceJarmusch you wanna accumulation the params of function sum, but you don't know the count of the params. By the arguments in the function, you can get all the params like Array, but this is not Array. So you should use .call to execute reduce on it.
@AliceJarmusch In ES6, you shouldn't use arguments in the array, you should use ...args operator get all the params. You can try @Tresdin answer in babel.
|
0

Just for changing arguments to an array of an array like object, you could use Array.apply.

Read more:

function x() {
    return Array.apply(Array, arguments);
}

var array = x(1, 4, 7);

console.log(array);
console.log(typeof array);
console.log(Array.isArray(array));

Comments

0
var sum =function() {
    var args = [];
    args.push.apply(args, arguments);
    return args.reduce((a,b) => { return a+b}, 0); // ES6 arrow function
}
sum(1,2,3); // return 6

1 Comment

Please elaborate on how this code answers the question.
0

Try this ES6 solution.

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

You can see the MDN article of rest parameters for more details. It is basically used for retrieving the list of arguments in an array.

2 Comments

Isn't it just the same as I'm trying to do? As I understand, the only issue here is to convert args to a list, and apply .reduce() to it. And I don't really know what I can do with it.
@AliceJarmusch You don't need a function to convert arguments to an array. Rest parameters were designed for this task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.