简体   繁体   English

Javascript数组的总和函数问题

[英]Sum function issue with Javascript Array

This works for my multiply function but when I try it with a simple +=, it produces the array values in sequence vs. adding them? 这适用于我的乘法函数,但是当我使用简单的+ =尝试时,它会按顺序生成数组值,而不是将它们相加?

 //sum() function sum() { var val1 = document.getElementById('sumMulti').value; var array = val1.split(','); var arraySum = 0; for (var i = 0; i < array.length; i++) { arraySum += array[i]; } document.getElementById('displayLabel').innerHTML = "The sum of your numbers is: " + arraySum.toString() + "."; } 
 <div class="col-md-4"> <label class="control-label">Enter Numbers Seperated by ",":</label> <br /> <input type="text" id="sumMulti" class=" form-control" /> <button type="submit" onclick="sum()" class="btn btn-primary pull-right" style="margin:20px 60px 10px 0px;">Sum()</button> <div id="displayLabel" class="control-label"></div> </div> 

You need to parse the numbers into integers. 您需要将数字解析为整数。

 //sum() function sum() { var val1 = document.getElementById('sumMulti').value; var array = val1.split(','); var arraySum = 0; for (var i = 0; i < array.length; i++) { arraySum += parseInt( array[i] ); } document.getElementById('displayLabel').innerHTML = "The sum of your numbers is: " + arraySum.toString() + "."; } 
 <div class="col-md-4"> <label class="control-label">Enter Numbers Seperated by ",":</label> <br /> <input type="text" id="sumMulti" class=" form-control" /> <button type="submit" onclick="sum()" class="btn btn-primary pull-right" style="margin:20px 60px 10px 0px;">Sum()</button> <div id="displayLabel" class="control-label"></div> </div> 

You need to learn to love the parseInt function. 您需要学习喜欢parseInt函数。

The default radix is 10, but you can specify other bases as well (for example, sometimes it's handy to parse hexadecimal with parseInt(hexString, 16) ). 默认基数为10,但您也可以指定其他基数(例如,有时使用parseInt(hexString, 16)解析十六进制很方便)。

If you expect real numbers, you may prefer parseFloat instead. 如果您期望实数,则可能更喜欢parseFloat It takes the same parameters. 它采用相同的参数。

As to why this worked for multiplication, Javascript is notorious for doing type conversions behind your back. 至于为什么它可以进行乘法运算,Javascript在背后进行类型转换是众所周知的。 Strings have a + operator (concatenate), so when you get the value of an input (which is a string before parsing) it will perform concatenation. 字符串具有+运算符(并置),因此,当您获得输入值(解析前为字符串)时,它将执行并置。 However, strings do not have a * operator. 但是,字符串没有 *运算符。 Behind your back, the Javascript runtime attempts to parse the strings as decimal numbers and then multiply. 在您的背后,JavaScript运行时尝试将字符串解析为十进制数字,然后相乘。 This is not behavior I would count on, so I'd prefer to explicitly parse all inputs first and then perform arithmetic operations on them. 这不是我要指望的行为,因此我更愿意先明确地解析所有输入,然后对它们执行算术运算。

 //sum() function sum() { var val1 = document.getElementById('sumMulti').value; var array = val1.split(','); var arraySum = 0; for (var i = 0; i < array.length; i++) { arraySum += parseInt(array[i], 10); } document.getElementById('displayLabel').innerHTML = "The sum of your numbers is: " + arraySum.toString() + "."; } 
 <div class="col-md-4"> <label class="control-label">Enter Numbers Seperated by ",":</label> <br /> <input type="text" id="sumMulti" class=" form-control" /> <button type="submit" onclick="sum()" class="btn btn-primary pull-right" style="margin:20px 60px 10px 0px;">Sum()</button> <div id="displayLabel" class="control-label"></div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM