javascript 中数字数组的总和
Summation of an array of numbers in javascript
在这里,我想听取用户的意见。拆分并将它们存储到数组中。然后显示数组数字的总和。
求和实验
<!-- for taking inputs -->
<input id="seriesInput" type="text" placeholder="please use space">
<!-- for submitting inputs -->
<input id="submiting" onclick="seriesFunction()" value="Submit" type="button">
<!-- for placing results -->
<div> Summation is <span id="sum"></span> </div>
<script type="text/javascript">
function seriesFunction()
{
value = document.getElementById("seriesInput").value;
// splitting string in an array
value_arr = value.split(" ");
alert(value_arr);
// calling reduce method for summation
var sum = value_arr.reduce(get_sum,0);
// assigning result
document.getElementById("sum").innerHTML =sum;
alert(sum);
function get_sum(total, currentVal) {
total += currentVal;
return total;
}
}
</script>
您正在 get_sum 函数中获取字符串形式的 currentValue,请尝试将其设为整数。你可以这样做:
function get_sum(total, currentVal) {
currentVal = parseInt(currentVal);
total += currentVal;
return total;
}
您需要将代码修改为:
function get_sum(total, currentVal) {
total += +currentVal;
return total;
}
注意+
在currentVal
前面。这会将字符串转换为数字,而不仅仅是 int
。所以好处是,如果您输入三个值,如 1.1 1.2 1.3,您将得到总和为 6.6。
早些时候没有这个你正在做字符串连接而不是你打算做的总和。
function seriesFunction() {
value = document.getElementById("seriesInput").value;
// splitting string in an array
value_arr = value.split(" ");
alert(value_arr);
// calling reduce method for summation
var sum = value_arr.reduce(get_sum, 0);
// assigning result
document.getElementById("sum").innerHTML = sum;
alert(sum);
function get_sum(total, currentVal) {
total += +currentVal;
return total;
}
}
<input id="seriesInput" type="text" placeholder="please use space">
<!-- for submitting inputs -->
<input id="submiting" onclick="seriesFunction()" value="Submit" type="button">
<!-- for placing results -->
<div> Summation is <span id="sum"></span> </div>
在这里,我想听取用户的意见。拆分并将它们存储到数组中。然后显示数组数字的总和。
求和实验
<!-- for taking inputs -->
<input id="seriesInput" type="text" placeholder="please use space">
<!-- for submitting inputs -->
<input id="submiting" onclick="seriesFunction()" value="Submit" type="button">
<!-- for placing results -->
<div> Summation is <span id="sum"></span> </div>
<script type="text/javascript">
function seriesFunction()
{
value = document.getElementById("seriesInput").value;
// splitting string in an array
value_arr = value.split(" ");
alert(value_arr);
// calling reduce method for summation
var sum = value_arr.reduce(get_sum,0);
// assigning result
document.getElementById("sum").innerHTML =sum;
alert(sum);
function get_sum(total, currentVal) {
total += currentVal;
return total;
}
}
</script>
您正在 get_sum 函数中获取字符串形式的 currentValue,请尝试将其设为整数。你可以这样做:
function get_sum(total, currentVal) {
currentVal = parseInt(currentVal);
total += currentVal;
return total;
}
您需要将代码修改为:
function get_sum(total, currentVal) {
total += +currentVal;
return total;
}
注意+
在currentVal
前面。这会将字符串转换为数字,而不仅仅是 int
。所以好处是,如果您输入三个值,如 1.1 1.2 1.3,您将得到总和为 6.6。
早些时候没有这个你正在做字符串连接而不是你打算做的总和。
function seriesFunction() {
value = document.getElementById("seriesInput").value;
// splitting string in an array
value_arr = value.split(" ");
alert(value_arr);
// calling reduce method for summation
var sum = value_arr.reduce(get_sum, 0);
// assigning result
document.getElementById("sum").innerHTML = sum;
alert(sum);
function get_sum(total, currentVal) {
total += +currentVal;
return total;
}
}
<input id="seriesInput" type="text" placeholder="please use space">
<!-- for submitting inputs -->
<input id="submiting" onclick="seriesFunction()" value="Submit" type="button">
<!-- for placing results -->
<div> Summation is <span id="sum"></span> </div>