如何查找用户输入的数组总和?

时间:2016-11-04 05:47:51

标签: javascript arrays html5 function sum

如何找到用户在numbersBox中输入的数字总和? 如果用户输入了" 5,3,6,1和#34;在numbersBox中,我应该得到答案" 15"单击"查找总和"后sumBox按钮。我无法弄清楚如何添加所有数组元素以及我应该在for循环中添加什么。

function findSUM(){
    list = numbersBox.value.split(",");

    var sum = list[0];
    for(var i = 0; i < list.length; i++){
        list[i] = parseInt(list[i]);

        if(list[i] < i){ 
            max += list[i];
        }
    }
    sumBox.value = sum; 
} 
<label>Write numbers separated by commas:</label>
<br><br>
<input type="text" id="numbersBox" />

<label>Sum of all numbers:</label>

<input type="text" id="sumBox" />

<button type="button" onclick="findSUM();">Find Sum</button>

谢谢。我非常感谢你的帮助。

6 个答案:

答案 0 :(得分:1)

我认为你对循环进行了过度设计,只需将值分开并在遍历数组时将它们加到sum中。

&#13;
&#13;
function findSUM(){
    list = numbersBox.value.split(",");
    console.log(list);
    var sum = 0;
    for(var i = 0; i < list.length; i++){
        sum += !isNaN(list[i]) ? +list[i] : 0 ; // here '+' would cast the string to number
    }
    sumBox.value = sum; 
}
&#13;
<label>Write numbers separated by commas:</label>
<br><br>
<input type="text" id="numbersBox" />

<label>Sum of all numbers:</label>

<input type="text" id="sumBox" />

<button type="button" onclick="findSUM();">Find Sum</button>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您需要将所有数组元素转换为Number,此处Array.prototype.map()可以使用。迭代应从第二个索引开始,因为变量sum初始化为值,如果第一个索引处的数字。

function findSUM() {
  //Create a list of Numbers
  var list = document.getElementById('numbersBox').value
    .split(",")
    .map(function(x) {
      return isNaN(x) ? 0 : Number(x);
    });

  //Init with first no
  var sum = list[0];

  //Iteration starts wit 2nd element
  for (var i = 1; i < list.length; i++) {
    sum += list[i];
  }
  document.getElementById('sumBox').value = sum;
}
<label>Write numbers separated by commas:</label>
<br>
<br>
<input type="text" id="numbersBox" />
<label>Sum of all numbers:</label>
<input type="text" id="sumBox" />
<button type="button" onclick="findSUM();">Find Sum</button>

答案 2 :(得分:1)

使用此代码..

 list = [12,56,9];
 var Total =  list.reduce(function(a,b){
   return a+b;
 });

答案 3 :(得分:1)

要查找用户输入的输入中的数字总和,即"5, 3, 6, 1",您可以将字符串拆分为,,并使用map函数将结果字符串数组转换为一个整数数组然后使用reduce函数通过使用一个添加两个操作数的简单函数来计算整数数组中数字的总和。

&#13;
&#13;
function findSum() {
  var element = document.getElementById('numbersBox');
  var sumBox = document.getElementById('sumBox');
  var numbers = processInput(element.value);
  var sum = getSum(numbers);
  sumBox.value = sum;
}

function processInput(input) {
  if (typeof input === 'string' || input instanceof String) {
    return input.split(',').map(function(currentValue, index, arr) {
      return !isNaN(currentValue) && currentValue.length ? parseInt(currentValue) : 0;
    });
  }
}

function getSum(numbers) {
  var sum = 0;

  if (numbers instanceof Array) {
    sum = numbers.reduce(add);
    // sum = numbers.reduce((a, b) => a + b, 0); // ES 2015
  }

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

  return sum;
}
&#13;
<label>Write numbers separated by commas:</label>
<br>
<br>
<input type="text" id="numbersBox" />

<label>Sum of all numbers:</label>

<input type="text" id="sumBox" />

<button type="button" onclick="findSum();">Find Sum</button>
&#13;
&#13;
&#13;

上面的代码具有处理/解析数据和计算总和的不同函数,下面的示例将所有操作分组到单个函数中,而不是分离职责。

&#13;
&#13;
function findSum() {
  var element = document.getElementById('numbersBox');
  var sumBox = document.getElementById('sumBox');
  var numbers = element.value.split(',');
  var sum = 0;
  for (var i = 0; i < numbers.length; i++) {
    var number = numbers[i];
    sum += !isNaN(number) && number.length ? parseInt(number) : 0;
  }

  sumBox.value = sum;
}
&#13;
<label>Write numbers separated by commas:</label>
<br>
<br>
<input type="text" id="numbersBox" />

<label>Sum of all numbers:</label>

<input type="text" id="sumBox" />

<button type="button" onclick="findSum();">Find Sum</button>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

...试

function findSUM(){
    list = numbersBox.value.split(",");
    var sum = parseInt(list[0]);
    for(var i = 1; i < list.length; i++){
       list[i] = parseInt(list[i]);
       sum += list[i];
    }
    sumBox.value = sum; 
} 

答案 5 :(得分:0)

一个很好的干净解决方案,易于阅读和理解,在整个过程中验证并支持浮点数而不仅仅是整数。

&#13;
&#13;
function findSUM()
{
  var list = document.getElementById("numbersBox").value.split(",");
  var sum = 0;
  for(var i=0;i<list.length;i++)
    if(list[i]&&!isNaN(list[i]))
      sum += parseFloat(list[i]);
  document.getElementById("sumBox").value = sum;
}
&#13;
<label>Write numbers separated by commas:</label>
<br>
<br>
<input type="text" id="numbersBox" />
<label>Sum of all numbers:</label>
<input type="text" id="sumBox" />
<button type="button" onclick="findSUM();">Find Sum</button>
&#13;
&#13;
&#13;