-2

如果我有两个总和变量和单个变量。有没有更简单的方法来获得总和?

例子:

var first_one = 5;
var first_two = 5;

var second_one = 5;
var second_two = 5;

var firstTotal = first_one + first_two;
var secondTotal = second_one + second_two;

var total = firstTotal + secondTotal;
return total;
4

2 回答 2

10

创建一个名为sum()

function sum(val1, val2) {
    return val1 + val2;
}

叫它

var firstTotal = sum(first_one, first_two);
var secondTotal = sum(second_one, second_two);

var total = sum(firstTotal, secondTotal);
于 2013-05-16T14:31:04.997 回答
1
function sum(a,b){
      return a+b;
}    
var firstTotal = sum(first_one, first_two);
于 2013-05-16T14:31:25.940 回答