在所有值数组和返回总和环

编程入门 行业动态 更新时间:2024-10-24 04:50:55
本文介绍了在所有值数组和返回总和环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

新手的问题(我曾经尝试尽我所能来修补这个在一起,没有错误,但我缺少的部分如下所示:我想要做的是有用户一个数字输入和数字的总和返回我的逻辑如下:用户输入的字符串,字符串数组通过分裂数组,循环,总结所有号码,回报总和。

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.

更多推荐

在所有值数组和返回总和环

本文发布于:2023-10-14 12:01:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1491005.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   总和

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!