admin管理员组文章数量:1415476
I just starting using JavaScript a couple weeks ago and still struggling a bit. I need to create a loop that calculates the sum of all values coming from a votes array in a separate .js file.
The function has a single parameter, votes, representing one of the five vote arrays (vote1 through vote5). Add the following commands to the function:
a. Declare a variable named total, setting its initial value to 0.
b. Create a for loop that loops through each of the items in the votes array, adding that item’s value to the total variable.
c. After the for loop is completed, return the value of the total variable from the function.
Heres my html file.
<script>
function totalVotes()
{
var total = 0;
for (i=0; i < votes.length; ++i)
{
total += votes[i];
}
return total;
}
</script>
I just starting using JavaScript a couple weeks ago and still struggling a bit. I need to create a loop that calculates the sum of all values coming from a votes array in a separate .js file.
The function has a single parameter, votes, representing one of the five vote arrays (vote1 through vote5). Add the following commands to the function:
a. Declare a variable named total, setting its initial value to 0.
b. Create a for loop that loops through each of the items in the votes array, adding that item’s value to the total variable.
c. After the for loop is completed, return the value of the total variable from the function.
Heres my html file.
<script>
function totalVotes()
{
var total = 0;
for (i=0; i < votes.length; ++i)
{
total += votes[i];
}
return total;
}
</script>
Share
Improve this question
edited Jul 12, 2017 at 19:07
CChristiansen
asked Jul 12, 2017 at 3:42
CChristiansenCChristiansen
471 gold badge1 silver badge6 bronze badges
4
|
5 Answers
Reset to default 10You 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));
I would suggest to create Javascript Object instead of array of different properties.If you use Object then you will have to create Array of Objects with following structure -
var first_object = {
'name':"Jeffrey Hart",
'party':"D",
'race':"1st Congressional District",
'vote':"45125"
}
You will create object for each record and use Arrays.push to add into your array. Lets assume array is records, then use
records.push(first_object)
After Adding all the records then you will have arrays of Objects now you can tracer through it with simple forEach loop
var total = 0;
records.forEach( function (record)
{
total+= record.vote;
});
If you are new to Javascript I would recommend to read
JavaScript: The Good Parts By Douglas Crockford
http://shop.oreilly.com/product/9780596517748.do
You can add the numbers like below
let total = 0;
for (let i = 0; i < this.state.seatArray.length; i++) {
total += total + parseInt(this.state.seatArray[i].price);
}
Thank you
You can add the numbers like below
for(let i = 0; i < product.length; i++)
{
var count1=+product[i].Pending;
var count2=+product[i].Competed;
this.totalCount=this.totalCount+(+count1)+(+count2);
}
Declare totalCount
globally on top totalCount:number=0;
.
本文标签:
版权声明:本文标题:html - JavaScript, Using a for loop to calculate the sum of all the values within an array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738696450a2107410.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
votes
as a parameter of the function, and the actual call (e.g.totalVotes(votes3)
). – Bergi Commented Jul 12, 2017 at 3:47var i = 0
orlet i = 0
instead ofi=0
. – zetavolt Commented Jul 12, 2017 at 4:03