var nums = ['100','300','400','60','40'];
var sum = 0;
for(var i=0; i < nums.length; i++){
sum += parseInt(nums[i]);
}
alert(sum);
Tested: http://jsfiddle.net/GYpd2/6/ (thanks to user1503606)
If nums contains numbers only there is no need for parseInt().
var nums = ['100','300','400','60','40'];
var sum = 0;
for(var i=0; i < nums.length; i++){
sum += parseInt(nums[i]);
}
alert(sum);
Tested: http://jsfiddle.net/GYpd2/6/ (thanks to user1503606)
If nums contains numbers only there is no need for parseInt().
Prime example for ES5's Array.prototype.reduce method. Like:
var nums = ['100','300','400','60','40'];
var total = nums.reduce(function(a,b) {
return (+a)+(+b);
});
Demo: http://jsfiddle.net/FwfmE/
I can't figure why my sum in this loop doesn't add up like it should.
const sumAll = function(a, b) {
for (let i = a; i < b; i++) {
let sum = 0
sum += i
console.log(sum)
// return sum
}
};
console.log(sumAll(1, 4))every iteration i become one higher integer and in this case 1 to 4, but looking at the console.log every iteration i keep getting addition of only 1?Also i'm not sure how to properly return in a loop because it returns after 1 loop, i tried using if ( i === b ) then return sum but it won't recoginize it from the if statement, the same is if you use return sum between the last 2 curly brackets.
the output of sumAll should be 1+2+3+4=10
If you got any tips i would love to hear them!
html - JavaScript, Using a for loop to calculate the sum of all the values within an array - Stack Overflow
With Javascript use a for loop to sum numbers in an array - Stack Overflow
javascript - Loop through array and return sum of all values - Stack Overflow
How do I write a function that should return true if ANY pair of numbers in an array add up to a target number? I’ve only figured it out for the first and last so far.
You can use reduce Method of array. Reduce Method gives concatenated value based on elements across the Array. For Example :
const sum = [1,2,3].reduce(function(result,item) {
return result + item;
}, 0);
console.log(sum);
The above code gives the output 6 that is the sum of given array. You can check other methods of array on my Gist: Iterate_Over_Array.js
You just need to pass in the array you want to iterate over:
var vote1 = [45125, 44498, 5143]
function totalVotes(votes) {
var total = 0;
for (var i = 0; i < votes.length; i++) {
total += votes[i];
}
return total;
}
// better alternative
function tallyVotes(votes) {
return votes.reduce((total, vote) => total + vote, 0);
}
console.log('for loop: ', totalVotes(vote1));
console.log('reduce: ', tallyVotes(vote1));
You want a basic loop to convert and add each item.
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).
If you want the numbers to only be integers (no decimals), change parseFloat to parseInt.
Here is an outline, but I think it's worth writing it yourself to learn.
- split() returns an array of characters. You can get the length of that array with temp.length
- You'll want to loop over every element of that array.
- Because "1" + "2" = "12" in javascript, you need to convert your characters to integers
- Keep a running total of the sums, and add to it at each iteration of the loop, maybe validating that each number is a real number