I have a series of input boxes into which users will input numerical values to be stored in an array. I would like for these values to be added and their sum shown via an alert. However I cannot determine how to pass these values to the array via the click of a button like I hope to. Here is my code:
$('#check').click(function() {
var arr = [];
getSum = function() {
for ( i = 0; i < 100; i++){
arr[i] =
$('input:number').map(function(){
return this.value;
});
}
var sum = 0;
var n = arr.length;
for(var i = 0; i < n ; i++) {
sum += parseInt(arr[i]);
}
alert(sum);
}
getSum();
});
with HTML markup:
<input type="number" id="field1" />
<input type="number" id="field2" />
<input type="number" id="field3" />
<button type="button" id="check">Calc</button>
Also, I have figured out how to dynamically add inputs so that the user may include more values in the sum, but I am not sure how this would affect the code. Would this still be sufficient?
:numberdoesn't exist AFAIK andmap()returns an array. You are over complicating things