Calculate the sum of an array
I have created an array using the following JavaScript:
for (i = 0; i < numOfPeriodsCost; i++) {
esclatedPrice *= (1 + annualElecIncrease)
rateArray.push(esclatedPrice);
annualCostA = consumptionA * rateArray[i]
console.log("annualCostA= " + annualCostA);
}
For context:
var numOfPeriodsCost = 10;
var annualElecIncrease = 0.1;
var consumptionA = 520;
The console logs the following for annualCostA:
How would I take this one step further and calculate the sum of the array?
Thanks
javascript arrays sum
add a comment |
I have created an array using the following JavaScript:
for (i = 0; i < numOfPeriodsCost; i++) {
esclatedPrice *= (1 + annualElecIncrease)
rateArray.push(esclatedPrice);
annualCostA = consumptionA * rateArray[i]
console.log("annualCostA= " + annualCostA);
}
For context:
var numOfPeriodsCost = 10;
var annualElecIncrease = 0.1;
var consumptionA = 520;
The console logs the following for annualCostA:
How would I take this one step further and calculate the sum of the array?
Thanks
javascript arrays sum
What is the initial value ofesclatedPrice
?
– Gary Thomas
Nov 20 '18 at 9:51
add a comment |
I have created an array using the following JavaScript:
for (i = 0; i < numOfPeriodsCost; i++) {
esclatedPrice *= (1 + annualElecIncrease)
rateArray.push(esclatedPrice);
annualCostA = consumptionA * rateArray[i]
console.log("annualCostA= " + annualCostA);
}
For context:
var numOfPeriodsCost = 10;
var annualElecIncrease = 0.1;
var consumptionA = 520;
The console logs the following for annualCostA:
How would I take this one step further and calculate the sum of the array?
Thanks
javascript arrays sum
I have created an array using the following JavaScript:
for (i = 0; i < numOfPeriodsCost; i++) {
esclatedPrice *= (1 + annualElecIncrease)
rateArray.push(esclatedPrice);
annualCostA = consumptionA * rateArray[i]
console.log("annualCostA= " + annualCostA);
}
For context:
var numOfPeriodsCost = 10;
var annualElecIncrease = 0.1;
var consumptionA = 520;
The console logs the following for annualCostA:
How would I take this one step further and calculate the sum of the array?
Thanks
javascript arrays sum
javascript arrays sum
edited Nov 20 '18 at 10:12
Eugene Mihaylin
9681424
9681424
asked Nov 20 '18 at 9:47
JohnGISJohnGIS
828
828
What is the initial value ofesclatedPrice
?
– Gary Thomas
Nov 20 '18 at 9:51
add a comment |
What is the initial value ofesclatedPrice
?
– Gary Thomas
Nov 20 '18 at 9:51
What is the initial value of
esclatedPrice
?– Gary Thomas
Nov 20 '18 at 9:51
What is the initial value of
esclatedPrice
?– Gary Thomas
Nov 20 '18 at 9:51
add a comment |
7 Answers
7
active
oldest
votes
In your loop, you just need to add a variable sum
that will add the value of annualCostA
in each iteration like so:
var sum = 0;
for (i = 0; i < numOfPeriodsCost; i++) {
esclatedPrice *= (1 + annualElecIncrease)
rateArray.push(esclatedPrice);
annualCostA = consumptionA * rateArray[i]
sum += annualCostA // add the annualCostA value in each iteration
console.log("annualCostA= " + annualCostA);
}
console.log(sum); //total sum
Your solution worked perfectly for my situation. Thanks so much, it was awesome to learn about this :)
– JohnGIS
Nov 20 '18 at 14:07
add a comment |
You can use Array.prototype.reduce():
var sumOfRateArray = rateArray.reduce((a, c) => a + c, 0);
Code:
const rateArray = [12.100000000000001,13.310000000000002,14.641000000000004,16.105100000000004,17.715610000000005,19.487171000000007,21.43588810000001,23.579476910000015,25.937424601000018];
const sumOfRateArray = rateArray.reduce((a, c) => a + c, 0);
console.log(sumOfRateArray);
add a comment |
Add a sum variable before the loop let sum=0;
Add the element you want to sum inside the loop sum+= your_element;
Print the result console.log("Sum= "+sum);
add a comment |
Using Array.prototype.reduce() on your rateArray
variable after the loop:
var rateArray = [
936,
1029.6,
1132.56
];
var arraySum = rateArray.reduce((total, e) => total + e);
console.log(arraySum)
add a comment |
var numOfPeriodsCost = 10;
var annualElecIncrease = 0.1;
var consumptionA = 520;
let sum = 0;
for (i = 0; i < numOfPeriodsCost; i++) {
esclatedPrice *= (1 + annualElecIncrease);
rateArray.push(esclatedPrice);
annualCostA = consumptionA * rateArray[i]
console.log("annualCostA= " + annualCostA);
sum += annualCostA;
}
console.log('sum', sum);
add a comment |
The following is the forEach
approach:
let numbers = [1, 2, 3, 4];
let sum = 0;
numbers.forEach((item, index)=>{sum= sum + item;});
document.write("Sum: " + sum);
Test: link
Doc: link
add a comment |
I think there is something wrong with your yearly calculation, should it be consumption + (years*increase*consumption)
?
You can map an array of 0-numOfPeriodsCost to yearly costs and reduce that to an array containing costs for that year and total until that year. Then map those values into objects containing total and year:
const numOfPeriodsCost = 10;
const annualElecIncrease = 0.1;
const consumptionA = 520;
console.log(
[...new Array(numOfPeriodsCost)]
.map(
(_, period) =>
consumptionA +
period * annualElecIncrease * consumptionA,
)
.reduce(
([sum, result], item) => [
sum + item,
result.concat([[sum + item, item]]),
],
[0, ],
)[1]
.map(([total, year]) => ({ total, year })),
);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390229%2fcalculate-the-sum-of-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
In your loop, you just need to add a variable sum
that will add the value of annualCostA
in each iteration like so:
var sum = 0;
for (i = 0; i < numOfPeriodsCost; i++) {
esclatedPrice *= (1 + annualElecIncrease)
rateArray.push(esclatedPrice);
annualCostA = consumptionA * rateArray[i]
sum += annualCostA // add the annualCostA value in each iteration
console.log("annualCostA= " + annualCostA);
}
console.log(sum); //total sum
Your solution worked perfectly for my situation. Thanks so much, it was awesome to learn about this :)
– JohnGIS
Nov 20 '18 at 14:07
add a comment |
In your loop, you just need to add a variable sum
that will add the value of annualCostA
in each iteration like so:
var sum = 0;
for (i = 0; i < numOfPeriodsCost; i++) {
esclatedPrice *= (1 + annualElecIncrease)
rateArray.push(esclatedPrice);
annualCostA = consumptionA * rateArray[i]
sum += annualCostA // add the annualCostA value in each iteration
console.log("annualCostA= " + annualCostA);
}
console.log(sum); //total sum
Your solution worked perfectly for my situation. Thanks so much, it was awesome to learn about this :)
– JohnGIS
Nov 20 '18 at 14:07
add a comment |
In your loop, you just need to add a variable sum
that will add the value of annualCostA
in each iteration like so:
var sum = 0;
for (i = 0; i < numOfPeriodsCost; i++) {
esclatedPrice *= (1 + annualElecIncrease)
rateArray.push(esclatedPrice);
annualCostA = consumptionA * rateArray[i]
sum += annualCostA // add the annualCostA value in each iteration
console.log("annualCostA= " + annualCostA);
}
console.log(sum); //total sum
In your loop, you just need to add a variable sum
that will add the value of annualCostA
in each iteration like so:
var sum = 0;
for (i = 0; i < numOfPeriodsCost; i++) {
esclatedPrice *= (1 + annualElecIncrease)
rateArray.push(esclatedPrice);
annualCostA = consumptionA * rateArray[i]
sum += annualCostA // add the annualCostA value in each iteration
console.log("annualCostA= " + annualCostA);
}
console.log(sum); //total sum
answered Nov 20 '18 at 9:55
Ahsan AliAhsan Ali
2,435821
2,435821
Your solution worked perfectly for my situation. Thanks so much, it was awesome to learn about this :)
– JohnGIS
Nov 20 '18 at 14:07
add a comment |
Your solution worked perfectly for my situation. Thanks so much, it was awesome to learn about this :)
– JohnGIS
Nov 20 '18 at 14:07
Your solution worked perfectly for my situation. Thanks so much, it was awesome to learn about this :)
– JohnGIS
Nov 20 '18 at 14:07
Your solution worked perfectly for my situation. Thanks so much, it was awesome to learn about this :)
– JohnGIS
Nov 20 '18 at 14:07
add a comment |
You can use Array.prototype.reduce():
var sumOfRateArray = rateArray.reduce((a, c) => a + c, 0);
Code:
const rateArray = [12.100000000000001,13.310000000000002,14.641000000000004,16.105100000000004,17.715610000000005,19.487171000000007,21.43588810000001,23.579476910000015,25.937424601000018];
const sumOfRateArray = rateArray.reduce((a, c) => a + c, 0);
console.log(sumOfRateArray);
add a comment |
You can use Array.prototype.reduce():
var sumOfRateArray = rateArray.reduce((a, c) => a + c, 0);
Code:
const rateArray = [12.100000000000001,13.310000000000002,14.641000000000004,16.105100000000004,17.715610000000005,19.487171000000007,21.43588810000001,23.579476910000015,25.937424601000018];
const sumOfRateArray = rateArray.reduce((a, c) => a + c, 0);
console.log(sumOfRateArray);
add a comment |
You can use Array.prototype.reduce():
var sumOfRateArray = rateArray.reduce((a, c) => a + c, 0);
Code:
const rateArray = [12.100000000000001,13.310000000000002,14.641000000000004,16.105100000000004,17.715610000000005,19.487171000000007,21.43588810000001,23.579476910000015,25.937424601000018];
const sumOfRateArray = rateArray.reduce((a, c) => a + c, 0);
console.log(sumOfRateArray);
You can use Array.prototype.reduce():
var sumOfRateArray = rateArray.reduce((a, c) => a + c, 0);
Code:
const rateArray = [12.100000000000001,13.310000000000002,14.641000000000004,16.105100000000004,17.715610000000005,19.487171000000007,21.43588810000001,23.579476910000015,25.937424601000018];
const sumOfRateArray = rateArray.reduce((a, c) => a + c, 0);
console.log(sumOfRateArray);
const rateArray = [12.100000000000001,13.310000000000002,14.641000000000004,16.105100000000004,17.715610000000005,19.487171000000007,21.43588810000001,23.579476910000015,25.937424601000018];
const sumOfRateArray = rateArray.reduce((a, c) => a + c, 0);
console.log(sumOfRateArray);
const rateArray = [12.100000000000001,13.310000000000002,14.641000000000004,16.105100000000004,17.715610000000005,19.487171000000007,21.43588810000001,23.579476910000015,25.937424601000018];
const sumOfRateArray = rateArray.reduce((a, c) => a + c, 0);
console.log(sumOfRateArray);
edited Nov 24 '18 at 2:42
answered Nov 20 '18 at 9:56
Yosvel QuinteroYosvel Quintero
11.2k42430
11.2k42430
add a comment |
add a comment |
Add a sum variable before the loop let sum=0;
Add the element you want to sum inside the loop sum+= your_element;
Print the result console.log("Sum= "+sum);
add a comment |
Add a sum variable before the loop let sum=0;
Add the element you want to sum inside the loop sum+= your_element;
Print the result console.log("Sum= "+sum);
add a comment |
Add a sum variable before the loop let sum=0;
Add the element you want to sum inside the loop sum+= your_element;
Print the result console.log("Sum= "+sum);
Add a sum variable before the loop let sum=0;
Add the element you want to sum inside the loop sum+= your_element;
Print the result console.log("Sum= "+sum);
answered Nov 20 '18 at 9:51
Kristjan KicaKristjan Kica
2,1971926
2,1971926
add a comment |
add a comment |
Using Array.prototype.reduce() on your rateArray
variable after the loop:
var rateArray = [
936,
1029.6,
1132.56
];
var arraySum = rateArray.reduce((total, e) => total + e);
console.log(arraySum)
add a comment |
Using Array.prototype.reduce() on your rateArray
variable after the loop:
var rateArray = [
936,
1029.6,
1132.56
];
var arraySum = rateArray.reduce((total, e) => total + e);
console.log(arraySum)
add a comment |
Using Array.prototype.reduce() on your rateArray
variable after the loop:
var rateArray = [
936,
1029.6,
1132.56
];
var arraySum = rateArray.reduce((total, e) => total + e);
console.log(arraySum)
Using Array.prototype.reduce() on your rateArray
variable after the loop:
var rateArray = [
936,
1029.6,
1132.56
];
var arraySum = rateArray.reduce((total, e) => total + e);
console.log(arraySum)
var rateArray = [
936,
1029.6,
1132.56
];
var arraySum = rateArray.reduce((total, e) => total + e);
console.log(arraySum)
var rateArray = [
936,
1029.6,
1132.56
];
var arraySum = rateArray.reduce((total, e) => total + e);
console.log(arraySum)
answered Nov 20 '18 at 9:56
Gary ThomasGary Thomas
1,1351412
1,1351412
add a comment |
add a comment |
var numOfPeriodsCost = 10;
var annualElecIncrease = 0.1;
var consumptionA = 520;
let sum = 0;
for (i = 0; i < numOfPeriodsCost; i++) {
esclatedPrice *= (1 + annualElecIncrease);
rateArray.push(esclatedPrice);
annualCostA = consumptionA * rateArray[i]
console.log("annualCostA= " + annualCostA);
sum += annualCostA;
}
console.log('sum', sum);
add a comment |
var numOfPeriodsCost = 10;
var annualElecIncrease = 0.1;
var consumptionA = 520;
let sum = 0;
for (i = 0; i < numOfPeriodsCost; i++) {
esclatedPrice *= (1 + annualElecIncrease);
rateArray.push(esclatedPrice);
annualCostA = consumptionA * rateArray[i]
console.log("annualCostA= " + annualCostA);
sum += annualCostA;
}
console.log('sum', sum);
add a comment |
var numOfPeriodsCost = 10;
var annualElecIncrease = 0.1;
var consumptionA = 520;
let sum = 0;
for (i = 0; i < numOfPeriodsCost; i++) {
esclatedPrice *= (1 + annualElecIncrease);
rateArray.push(esclatedPrice);
annualCostA = consumptionA * rateArray[i]
console.log("annualCostA= " + annualCostA);
sum += annualCostA;
}
console.log('sum', sum);
var numOfPeriodsCost = 10;
var annualElecIncrease = 0.1;
var consumptionA = 520;
let sum = 0;
for (i = 0; i < numOfPeriodsCost; i++) {
esclatedPrice *= (1 + annualElecIncrease);
rateArray.push(esclatedPrice);
annualCostA = consumptionA * rateArray[i]
console.log("annualCostA= " + annualCostA);
sum += annualCostA;
}
console.log('sum', sum);
answered Nov 20 '18 at 9:56
Eugene MihaylinEugene Mihaylin
9681424
9681424
add a comment |
add a comment |
The following is the forEach
approach:
let numbers = [1, 2, 3, 4];
let sum = 0;
numbers.forEach((item, index)=>{sum= sum + item;});
document.write("Sum: " + sum);
Test: link
Doc: link
add a comment |
The following is the forEach
approach:
let numbers = [1, 2, 3, 4];
let sum = 0;
numbers.forEach((item, index)=>{sum= sum + item;});
document.write("Sum: " + sum);
Test: link
Doc: link
add a comment |
The following is the forEach
approach:
let numbers = [1, 2, 3, 4];
let sum = 0;
numbers.forEach((item, index)=>{sum= sum + item;});
document.write("Sum: " + sum);
Test: link
Doc: link
The following is the forEach
approach:
let numbers = [1, 2, 3, 4];
let sum = 0;
numbers.forEach((item, index)=>{sum= sum + item;});
document.write("Sum: " + sum);
Test: link
Doc: link
answered Nov 20 '18 at 9:59
Tu.maTu.ma
807219
807219
add a comment |
add a comment |
I think there is something wrong with your yearly calculation, should it be consumption + (years*increase*consumption)
?
You can map an array of 0-numOfPeriodsCost to yearly costs and reduce that to an array containing costs for that year and total until that year. Then map those values into objects containing total and year:
const numOfPeriodsCost = 10;
const annualElecIncrease = 0.1;
const consumptionA = 520;
console.log(
[...new Array(numOfPeriodsCost)]
.map(
(_, period) =>
consumptionA +
period * annualElecIncrease * consumptionA,
)
.reduce(
([sum, result], item) => [
sum + item,
result.concat([[sum + item, item]]),
],
[0, ],
)[1]
.map(([total, year]) => ({ total, year })),
);
add a comment |
I think there is something wrong with your yearly calculation, should it be consumption + (years*increase*consumption)
?
You can map an array of 0-numOfPeriodsCost to yearly costs and reduce that to an array containing costs for that year and total until that year. Then map those values into objects containing total and year:
const numOfPeriodsCost = 10;
const annualElecIncrease = 0.1;
const consumptionA = 520;
console.log(
[...new Array(numOfPeriodsCost)]
.map(
(_, period) =>
consumptionA +
period * annualElecIncrease * consumptionA,
)
.reduce(
([sum, result], item) => [
sum + item,
result.concat([[sum + item, item]]),
],
[0, ],
)[1]
.map(([total, year]) => ({ total, year })),
);
add a comment |
I think there is something wrong with your yearly calculation, should it be consumption + (years*increase*consumption)
?
You can map an array of 0-numOfPeriodsCost to yearly costs and reduce that to an array containing costs for that year and total until that year. Then map those values into objects containing total and year:
const numOfPeriodsCost = 10;
const annualElecIncrease = 0.1;
const consumptionA = 520;
console.log(
[...new Array(numOfPeriodsCost)]
.map(
(_, period) =>
consumptionA +
period * annualElecIncrease * consumptionA,
)
.reduce(
([sum, result], item) => [
sum + item,
result.concat([[sum + item, item]]),
],
[0, ],
)[1]
.map(([total, year]) => ({ total, year })),
);
I think there is something wrong with your yearly calculation, should it be consumption + (years*increase*consumption)
?
You can map an array of 0-numOfPeriodsCost to yearly costs and reduce that to an array containing costs for that year and total until that year. Then map those values into objects containing total and year:
const numOfPeriodsCost = 10;
const annualElecIncrease = 0.1;
const consumptionA = 520;
console.log(
[...new Array(numOfPeriodsCost)]
.map(
(_, period) =>
consumptionA +
period * annualElecIncrease * consumptionA,
)
.reduce(
([sum, result], item) => [
sum + item,
result.concat([[sum + item, item]]),
],
[0, ],
)[1]
.map(([total, year]) => ({ total, year })),
);
const numOfPeriodsCost = 10;
const annualElecIncrease = 0.1;
const consumptionA = 520;
console.log(
[...new Array(numOfPeriodsCost)]
.map(
(_, period) =>
consumptionA +
period * annualElecIncrease * consumptionA,
)
.reduce(
([sum, result], item) => [
sum + item,
result.concat([[sum + item, item]]),
],
[0, ],
)[1]
.map(([total, year]) => ({ total, year })),
);
const numOfPeriodsCost = 10;
const annualElecIncrease = 0.1;
const consumptionA = 520;
console.log(
[...new Array(numOfPeriodsCost)]
.map(
(_, period) =>
consumptionA +
period * annualElecIncrease * consumptionA,
)
.reduce(
([sum, result], item) => [
sum + item,
result.concat([[sum + item, item]]),
],
[0, ],
)[1]
.map(([total, year]) => ({ total, year })),
);
answered Nov 20 '18 at 10:26
HMRHMR
13.8k113898
13.8k113898
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390229%2fcalculate-the-sum-of-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is the initial value of
esclatedPrice
?– Gary Thomas
Nov 20 '18 at 9:51