Calculate the sum of an array












-1















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:



enter image description here



How would I take this one step further and calculate the sum of the array?



Thanks










share|improve this question

























  • What is the initial value of esclatedPrice?

    – Gary Thomas
    Nov 20 '18 at 9:51
















-1















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:



enter image description here



How would I take this one step further and calculate the sum of the array?



Thanks










share|improve this question

























  • What is the initial value of esclatedPrice?

    – Gary Thomas
    Nov 20 '18 at 9:51














-1












-1








-1


1






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:



enter image description here



How would I take this one step further and calculate the sum of the array?



Thanks










share|improve this question
















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:



enter image description here



How would I take this one step further and calculate the sum of the array?



Thanks







javascript arrays sum






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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

















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












7 Answers
7






active

oldest

votes


















1














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





share|improve this answer
























  • Your solution worked perfectly for my situation. Thanks so much, it was awesome to learn about this :)

    – JohnGIS
    Nov 20 '18 at 14:07



















1














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);








share|improve this answer

































    0














    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);






    share|improve this answer































      0














      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)








      share|improve this answer































        0














        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);





        share|improve this answer































          0














          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






          share|improve this answer































            0














            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 })),
            );








            share|improve this answer























              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
              });


              }
              });














              draft saved

              draft discarded


















              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









              1














              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





              share|improve this answer
























              • Your solution worked perfectly for my situation. Thanks so much, it was awesome to learn about this :)

                – JohnGIS
                Nov 20 '18 at 14:07
















              1














              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





              share|improve this answer
























              • Your solution worked perfectly for my situation. Thanks so much, it was awesome to learn about this :)

                – JohnGIS
                Nov 20 '18 at 14:07














              1












              1








              1







              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





              share|improve this answer













              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






              share|improve this answer












              share|improve this answer



              share|improve this answer










              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



















              • 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













              1














              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);








              share|improve this answer






























                1














                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);








                share|improve this answer




























                  1












                  1








                  1







                  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);








                  share|improve this answer















                  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);






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 24 '18 at 2:42

























                  answered Nov 20 '18 at 9:56









                  Yosvel QuinteroYosvel Quintero

                  11.2k42430




                  11.2k42430























                      0














                      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);






                      share|improve this answer




























                        0














                        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);






                        share|improve this answer


























                          0












                          0








                          0







                          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);






                          share|improve this answer













                          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);







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 20 '18 at 9:51









                          Kristjan KicaKristjan Kica

                          2,1971926




                          2,1971926























                              0














                              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)








                              share|improve this answer




























                                0














                                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)








                                share|improve this answer


























                                  0












                                  0








                                  0







                                  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)








                                  share|improve this answer













                                  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)






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Nov 20 '18 at 9:56









                                  Gary ThomasGary Thomas

                                  1,1351412




                                  1,1351412























                                      0














                                      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);





                                      share|improve this answer




























                                        0














                                        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);





                                        share|improve this answer


























                                          0












                                          0








                                          0







                                          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);





                                          share|improve this answer













                                          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);






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Nov 20 '18 at 9:56









                                          Eugene MihaylinEugene Mihaylin

                                          9681424




                                          9681424























                                              0














                                              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






                                              share|improve this answer




























                                                0














                                                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






                                                share|improve this answer


























                                                  0












                                                  0








                                                  0







                                                  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






                                                  share|improve this answer













                                                  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







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Nov 20 '18 at 9:59









                                                  Tu.maTu.ma

                                                  807219




                                                  807219























                                                      0














                                                      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 })),
                                                      );








                                                      share|improve this answer




























                                                        0














                                                        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 })),
                                                        );








                                                        share|improve this answer


























                                                          0












                                                          0








                                                          0







                                                          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 })),
                                                          );








                                                          share|improve this answer













                                                          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 })),
                                                          );






                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Nov 20 '18 at 10:26









                                                          HMRHMR

                                                          13.8k113898




                                                          13.8k113898






























                                                              draft saved

                                                              draft discarded




















































                                                              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.




                                                              draft saved


                                                              draft discarded














                                                              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





















































                                                              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







                                                              Popular posts from this blog

                                                              How to change which sound is reproduced for terminal bell?

                                                              Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                                                              Can I use Tabulator js library in my java Spring + Thymeleaf project?