新手的问题(我曾经尝试尽我所能来修补这个在一起,没有错误,但我缺少的部分如下所示:我想要做的是有用户一个数字输入和数字的总和返回我的逻辑如下:用户输入的字符串,字符串数组通过分裂数组,循环,总结所有号码,回报总和。
novice question (I have tried as best I can to patch this together without mistakes but am missing the parts as indicated below: What I want to do is have a numbers input by user and the sum of the numbers returned. My logic is as follows: User inputs string, string is split to array, loop through array and sum all numbers, return sum.
这里是code我到目前为止有:
And here is thecode 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>我将用谦卑承认,这主要是可能是错误的和AP preciate任何人的时间和精力。
I admit with humility that this is mostly probably wrong and appreciate anybody's time and effort.
OK,所以我也做了建议,我得到下面我code以下错误:
OK, So I have done the suggested, I get the following error on my code below:
消息:的document.getElementById(...)'为空或不是对象行:16字符:1code:0
Message: 'document.getElementById(...)' is null or not an object Line: 16 Char: 1 Code: 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>有什么建议?并感谢所有为是COM prehensive - 我读了两个例子,现在了解的过程!只是不知道为什么我有一个错误。
Any suggestions? And thanks to all for being comprehensive - I have read both examples and understand the process now! Just not sure why I have a mistake.
推荐答案您想一个基本的循环转换和增加每个项目。
You want a basic loop to convert and add each item.
我也清理你的HTML一吨。你没有任何适当的结束标记。我也改变了所有的'名'的属性'ID'属性,使'的getElementById'将正常工作,这是我错过了我的第一个通行证。
I have also cleaned up your HTML a ton. 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.
<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).
如果你想要的数字仅是整数(没有小数),parseFloat改为parseInt函数。
If you want the numbers to only be integers (no decimals), change parseFloat to parseInt.
更多推荐
在所有值数组和返回总和环
发布评论