[英]Loop through array and return sum of all values
What I want to do is have numbers inputted by user and the sum of the numbers returned. 我想要做的是用户输入的数字和返回的数字之和。 My logic is as follows: 我的逻辑如下:
And here is the code I have so far: 这是我到目前为止的代码:
<script type='text/javascript'>
var val=document.getElementById('userInput').value;
var temp=val.split(" ");
function sum() {
for(var i=0, MISSING THIS BIT
document.getElementById('resultSum').innerHTML=MISSING THIS BIT;
}
</script>
<form name="input">
<textarea name="userInput" rows=20 cols=20></textarea>
<input name="Run" type=Button value="run" onClick="sum()">
<form name="resultSum"><input type=Text>
I admit with humility that this is mostly probably wrong and appreciate anybody's time and effort. 我谦虚地承认这很可能是错误的,并且感谢任何人的时间和精力。
UPDATE: I have done as suggested and I get the following error on my code below: 更新:我已按照建议完成,我在下面的代码中收到以下错误:
Message: 'document.getElementById(...)' is null or not an object Line: 16 Char: 1 Code: 0 消息:'document.getElementById(...)'为null或不是对象行:16字符:1代码:0
<html>
<script type='text/javascript'>
function sum(){
var val = document.getElementById('userInput').value;
var temp = val.split(" ");
var total = 0;
var v;
for(var i = 0; i < temp.length; i++) {
v = parseFloat(temp[i]);
if (!isNaN(v)) total += v;
}
document.getElementById('resultSum').innerHTML=total;
}
</script>
<form name="input">
<textarea name="userInput" rows=20 cols=20></textarea>
<input name="Run" type=Button value="run" onClick="sum()">
<form name="resultSum"><input type=text>
<html>
Any suggestions? 有什么建议么? Thanks to all for being comprehensive - I have read both examples and understand the process now! 感谢所有人的全面 - 我已经阅读了这两个例子并立即了解了这个过程!
You want a basic loop to convert and add each item. 您需要一个基本循环来转换和添加每个项目。
I have also cleaned up your HTML a ton. 我还清理了你的HTML。 You didn't have any proper closing tags. 你没有任何正确的结束标签。 I have also changed all of the 'name' attributes to 'id' attributes so that 'getElementById' would work properly, which I missed on my first pass. 我还将所有'name'属性更改为'id'属性,以便'getElementById'能够正常工作,这是我在第一次传递时错过的。
<html>
<head>
<script type='text/javascript'>
function sum(){
var val = document.getElementById('userInput').value;
var temp = val.split(" ");
var total = 0;
var v;
for(var i = 0; i < temp.length; i++) {
v = parseFloat(temp[i]);
if (!isNaN(v)) total += v;
}
document.getElementById('resultSumValue').value = total;
}
</script>
</head>
<body>
<form id="input">
<textarea id="userInput" rows=20 cols=20></textarea>
<input id="Run" type=Button value="run" onClick="sum()" />
</form>
<form id="resultSum">
<input id="resultSumValue" type="text" />
</form>
</body>
</html>
This will also ignore any values that are 'NaN' (Not a Number). 这也将忽略任何'NaN'(非数字)的值。
If you want the numbers to only be integers (no decimals), change parseFloat to parseInt. 如果您希望数字只是整数(无小数),请将parseFloat更改为parseInt。
Here is an outline, but I think it's worth writing it yourself to learn. 这是一个大纲,但我认为值得自己写一下来学习。
First start by declaring a variable set to 0 before the for loop. 首先在for循环之前声明变量设置为0。 Iterate over each element in the array (array.length) adding to the variable you set before the for loop. 迭代数组中的每个元素(array.length),添加到for循环之前设置的变量。
<html>
<head>
<title>sum input</title>
<style>
p, textarea,input{font-size:24px}
textarea,input{text-align:right}
</style>
<script>
function sum(){
var val= document.getElementById('userInput').value.replace(/^\D+|D+$/g,'');
document.getElementById('resultSum').value= eval(val.replace(/\D+/g,'+'));
}
</script>
</head>
<body>
<p>Enter a series of numbers :<br>
<textarea id="userInput" rows="10" cols="10"></textarea><br>
<input id="Run" type="button" value="Sum" onClick="sum()">
<input id="resultSum" type="text" readOnly>
</p>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.