最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Return sum of all arguments passed to function - Stack Overflow

programmeradmin4浏览0评论

One way is to use arguments. I can loop over the arguments array and can return the sum of all the arguments passed.

function sum(){
  var sum =0; 
  for(var i=0;i<arguments.length;i++){
     sum += arguments[i];
  }
   return sum;
}
sum(1,2); // returns 3
sum(1,2,3); // returns 6

Is there any other way to do it without using loop?

One way is to use arguments. I can loop over the arguments array and can return the sum of all the arguments passed.

function sum(){
  var sum =0; 
  for(var i=0;i<arguments.length;i++){
     sum += arguments[i];
  }
   return sum;
}
sum(1,2); // returns 3
sum(1,2,3); // returns 6

Is there any other way to do it without using loop?

Share Improve this question edited Dec 5, 2016 at 8:42 Alexander Ciesielski 10.8k5 gold badges47 silver badges66 bronze badges asked Jul 25, 2016 at 11:40 Code GuruCode Guru 15.6k30 gold badges143 silver badges210 bronze badges 1
  • 4 const sum = (...args) => [...args].reduce((a, b) => a + b, 0); – zerkms Commented Jul 25, 2016 at 11:43
Add a comment  | 

8 Answers 8

Reset to default 12

Other people provided answers with redundant copying of arguments to an array that is to be thrown away in a moment.

Instead you can do everything in one step:

function sum() {
    return Array.prototype.reduce.call(arguments, function(a, b) {
        return a + b;
    }, 0);
}

If using ES2015 is an option you can have slightly nicer (subjective) implementation:

const sum = (...args) => [...args].reduce((a, b) => a + b, 0);

As zerkms said, You can use reduce function like this

alert([1, 2].reduce((a, b) => a + b, 0));
alert([1, 2, 3].reduce((a, b) => a + b, 0));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Use Array.prototype.slice.call to convert arguments into array, and use reduce to sum up numbers.

 function sum(){
   var total =  Array.prototype.slice.call(arguments).reduce(function(a, b) { 
   return a + b; 
   }, 0);
   return total;
}
console.log(sum(1,2)); // returns 3
console.log(sum(1,2,3)); // returns 6

Demo: https://jsfiddle.net/wru8mvxt/10/

One-liner with rest parameters:

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

sum(1, 3, 5, 7, 9); // 25

Arguments is an array like object in javascript. You can use reduce property to convert to sum it is elements.

function sum() {
  var args = Array.prototype.slice.call(arguments);
  var sum = args.reduce(function(a, b) {
    return a + b;
  })
  return sum;
}


console.log(sum(1, 2)); // returns 3
console.log(sum(1, 2, 3)); // returns 6

You can also use forEach method

JSFIDDLE

Here is my solution with ES15:

function sumArgs() {
    return [...arguments].reduce((acc,next)=>acc+next, 0)
};

console.log(sumArgs(1,2,3,4,5));

In simple way you you can achieve it by using following

function sum(){
  var total =  0;
  for(var i=0; i<arguments.length; i++){
     total += arguments[i];
  }
  return total;
}
console.log(sum(1, 2)); // returns 3
console.log(sum(1, 2, 3)); // returns 6

Or

Also by using Array.prototype.reduce.call() method.

function sum(){
   var total =  Array.prototype.reduce.call(arguments, function(a, b) { 
   return a + b; 
   });
   return total;
}      
console.log(sum(1, 2));    // returns 3
console.log(sum(1, 2, 3));   // returns 6

It's a bit of a late answer I guess but this easy method and logic will benefit others I'm sure.

var val = 0;
function sum(){
    for(i of arguments){
        val += i;
    }console.log(val)
}

sum(10,20,40,30,110) //Output: 210
发布评论

评论列表(0)

  1. 暂无评论