• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Sales Data in an Array - Javascript

cno

Coder
I need some help please, I have a sales data for a couple of years and i want to retrieve the below information using Javascript:

-The total number of months included in the dataset.
- The net total amount of Profit/Losses over the entire period.
- The average of the changes in Profit/Losses over the entire period: you will need to track what the total change in profits are from month to month and then find the average. / (Total/Number of months)
- The greatest increase in profits (date and amount) over the entire period.
- The greatest decrease in losses (date and amount) over the entire period
Here is the data :

  1. var finances = [ ['Jan-2010', 867884], ['Feb-2010', 984655], ['Mar-2010', 322013], ['Apr-2010', -69417], ['May-2010', 310503], ['Jun-2010', 522857], ['Jul-2010', 1033096], ['Aug-2010', 604885], ['Sep-2010', -216386], ['Oct-2010', 477532], ['Nov-2010', 893810], ['Dec-2010', -80353], ['Jan-2011', 779806], ['Feb-2011', -335203], ['Mar-2011', 697845], ['Apr-2011', 793163], ['May-2011', 485070], ['Jun-2011', 584122], ['Jul-2011', 62729], ['Aug-2011', 668179], ['Sep-2011', 899906], ['Oct-2011', 834719], ['Nov-2011', 132003], ['Dec-2011', 309978], ['Jan-2012', -755566], ['Feb-2012', 1170593], ['Mar-2012', 252788], ['Apr-2012', 1151518], ['May-2012', 817256], ['Jun-2012', 570757], ['Jul-2012', 506702], ['Aug-2012', -1022534], ['Sep-2012', 475062], ['Oct-2012', 779976], ['Nov-2012', 144175], ['Dec-2012', 542494], ['Jan-2013', 359333], ['Feb-2013', 321469], ['Mar-2013', 67780], ['Apr-2013', 471435], ['May-2013', 565603], ['Jun-2013', 872480], ['Jul-2013', 789480], ['Aug-2013', 999942], ['Sep-2013', -1196225], ['Oct-2013', 268997], ['Nov-2013', -687986], ['Dec-2013', 1150461], ['Jan-2014', 682458], ['Feb-2014', 617856], ['Mar-2014', 824098], ['Apr-2014', 581943], ['May-2014', 132864], ['Jun-2014', 448062], ['Jul-2014', 689161], ['Aug-2014', 800701], ['Sep-2014', 1166643], ['Oct-2014', 947333], ['Nov-2014', 578668], ['Dec-2014', 988505], ['Jan-2015', 1139715], ['Feb-2015', 1029471], ['Mar-2015', 687533], ['Apr-2015', -524626], ['May-2015', 158620], ['Jun-2015', 87795], ['Jul-2015', 423389], ['Aug-2015', 840723], ['Sep-2015', 568529], ['Oct-2015', 332067], ['Nov-2015', 989499], ['Dec-2015', 778237], ['Jan-2016', 650000], ['Feb-2016', -1100387], ['Mar-2016', -174946], ['Apr-2016', 757143], ['May-2016', 445709], ['Jun-2016', 712961], ['Jul-2016', -1163797], ['Aug-2016', 569899], ['Sep-2016', 768450], ['Oct-2016', 102685], ['Nov-2016', 795914], ['Dec-2016', 60988], ['Jan-2017', 138230], ['Feb-2017', 671099] ];

The code should print this :
  1. Financial Analysis ----------------
  2. Total Months: 86
  3. Total: $38382578
  4. Average Change: 7803.48
  5. Greatest Increase: Nov-2016: $795914
  6. Greatest Decrease: Jul-2016: $-1163797
I have managed to get the first two right but the rest is wrong:
Here is my code:
JavaScript:
console.log("Total months:", finances.length);
console.log
// Amount of profit over entire period
const financesArray = finances.map((el) => el[1]);
const total = financesArray.reduce(
    (accVal, curVal) => accVal + curVal,
    0);
Here is my code for the rest but the figures are wrong from what i should be getting:
 

Ordep-20

Active Coder
Ok first to uses sort functions you will need clean this values


JavaScript:
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style>
      * {
              box-sizing: border-box;
          }

          body {
              margin: 0px 130px;
              padding: 0;
              height: 100vh;
          }

          .container {
              width: 800px;
              margin: 0 auto;
          }

          #primary_header {
              display: flex;
              background-color: rgb(248, 109, 66);
              margin: 0px 0px;
              justify-content: center;
              font: bold;
          }

          #invoice {
              letter-spacing: 20px;
              color: white;
              font: Helvetica, Sans-Serif;
          }

          .logo {
              margin: 0px 5px;
          }

          .logo img {
              justify-content: center;
              height: 43px;
              width: 53px;
              margin: 10px;
              filter: invert(100%);
          }

          .secondary_header {
              display: flex;
              font-size: 1rem;
              background-color: rgb(233, 245, 250);
          }

          .info {
              margin: 30px 20px;
          }

          .info #c_name {
              font-weight: bold;
          }

          #meta {
              border-bottom: 1px solid black;
              margin: 0px 20px 0px 180px;
              width: 220px;
          }

          #meta td {
              text-align: right;
          }

          #meta td.meta-head {
              text-align: left;
              color: black;
              font: bold;
              font-size: 1rem;
          }

          .customer {
              background-color: rgb(233, 245, 250);
          }

          .customer p {
              margin: 0px 20px;
              padding-top: 30px;
              color: rgb(248, 109, 66);
          }

          #address {
              background-color: rgb(233, 245, 250);
              margin: 20px;
              padding: 10px 10px;
              width: 50%;
              height: 50px;
              font-size: 0.9rem;
              border-radius: 10px;
              resize: none;
              border: none;
          }

          #items {
              width: 80%;
              border-collapse: collapse;
              border-spacing: 0;
              border: 1px solid black;
          }

          #items td,
          th {
              border: 1px solid black;
          }

          #items {
              justify-content: center;
              width: 100%;
              margin: 0 0 0 0;
              font-family: Arial, Helvetica, sans-serif;
          }

          #items input,
          textarea {
              width: 120px;
              height: 30px;
          }

          table tr.item-row td {
              border: 1;
          }

          #items td.item-name {
              width: 140px;
          }

          textarea {
              resize: none;
          }

          .description {
              width: 300px;
          }

          #items textarea {
              margin-top: 4px;
          }

          #items .price {
              margin: 0px 9px;
              text-align: center;
          }

          #items td.description textarea,
          #items td.item-name input,
          #items td.cost input,
          #items td.qty input {
              width: 100%;
          }

          #items td.total-line {
              padding: 5px;
              border-right: 0;
              text-align: right;
          }

          #items td.total-value {
              border-left: 0;
              padding: 10px;
          }

          #items td.total-value {
              border-left: 0;
              padding: 10px;
          }

          #items td.balance {
              background: #eee;
          }

          #items td.blank {
              border: 0;
          }

          #terms {
              text-align: center;
              margin: 20px 0 0 0;
          }

          input:hover,
          input:focus,
          #items td.total-value input:hover,
          #items td.total-value input:focus,
          .delete:hover {
              background-color: #e2e2e2;
          }

          .success {
              background-color: #24b96f ;
          }

          .danger {
              background-color: #ff5722 ;
          }

          .action_container>* {
              border: none;
              outline: none;
              color: #fff;
              text-decoration: none;
              padding: 4px 10px;
              margin: 2px;
              cursor: pointer;
              transition: 0.3s ease-in-out;
          }

          .action_container>*:hover {
              filter: hue-rotate(-20deg) brightness(0.97);
              transform: scale(1.05);
              border-color: transparent;
              box-shadow: 0 1px 10px rgba(207, 207, 207, 0.267);
              border-radius: 2px;
          }

          .terms {
              text-align: center;
              text-transform: uppercase;
              font: 10px Helvetica, Sans-Serif;
              border-bottom: 1px solid black;
              padding: 8px;
              font-family: Verdana;
          }

          footer {
              height: 20px;
              width: 800px;
              margin: 0 auto;
              background-color: rgb(248, 109, 66);
          }



    </style>
<title>Ordened array</title>
    <p id='result'></p>
</head>

<body>
    <div class="container">
    </div>
    <footer id="footer "></footer>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js'></script>
    <script type="text/javascript">
      const finances = [ ['Jan-2010', 867884], ['Feb-2010', 984655], ['Mar-2010', 322013], ['Apr-2010', -69417], ['May-2010', 310503], ['Jun-2010', 522857], ['Jul-2010', 1033096], ['Aug-2010', 604885], ['Sep-2010', -216386], ['Oct-2010', 477532], ['Nov-2010', 893810], ['Dec-2010', -80353], ['Jan-2011', 779806], ['Feb-2011', -335203], ['Mar-2011', 697845], ['Apr-2011', 793163], ['May-2011', 485070], ['Jun-2011', 584122], ['Jul-2011', 62729], ['Aug-2011', 668179], ['Sep-2011', 899906], ['Oct-2011', 834719], ['Nov-2011', 132003], ['Dec-2011', 309978], ['Jan-2012', -755566], ['Feb-2012', 1170593], ['Mar-2012', 252788], ['Apr-2012', 1151518], ['May-2012', 817256], ['Jun-2012', 570757], ['Jul-2012', 506702], ['Aug-2012', -1022534], ['Sep-2012', 475062], ['Oct-2012', 779976], ['Nov-2012', 144175], ['Dec-2012', 542494], ['Jan-2013', 359333], ['Feb-2013', 321469], ['Mar-2013', 67780], ['Apr-2013', 471435], ['May-2013', 565603], ['Jun-2013', 872480], ['Jul-2013', 789480], ['Aug-2013', 999942], ['Sep-2013', -1196225], ['Oct-2013', 268997], ['Nov-2013', -687986], ['Dec-2013', 1150461], ['Jan-2014', 682458], ['Feb-2014', 617856], ['Mar-2014', 824098], ['Apr-2014', 581943], ['May-2014', 132864], ['Jun-2014', 448062], ['Jul-2014', 689161], ['Aug-2014', 800701], ['Sep-2014', 1166643], ['Oct-2014', 947333], ['Nov-2014', 578668], ['Dec-2014', 988505], ['Jan-2015', 1139715], ['Feb-2015', 1029471], ['Mar-2015', 687533], ['Apr-2015', -524626], ['May-2015', 158620], ['Jun-2015', 87795], ['Jul-2015', 423389], ['Aug-2015', 840723], ['Sep-2015', 568529], ['Oct-2015', 332067], ['Nov-2015', 989499], ['Dec-2015', 778237], ['Jan-2016', 650000], ['Feb-2016', -1100387], ['Mar-2016', -174946], ['Apr-2016', 757143], ['May-2016', 445709], ['Jun-2016', 712961], ['Jul-2016', -1163797], ['Aug-2016', 569899], ['Sep-2016', 768450], ['Oct-2016', 102685], ['Nov-2016', 795914], ['Dec-2016', 60988], ['Jan-2017', 138230], ['Feb-2017', 671099] ];
        
       let res = document.getElementById('result');   
       var finances_a={};
       let tmp;       
    
            finances.forEach(function(val, i)
            {
                 //res.innerHTML +='<br/>'+val[0]+':'+val[1]+'<br/>'; 
                 tmp = val[0].split('-');
                 res.innerHTML +='<br/>'+tmp[0]+" "+tmp[1]+':'+val[1]+'<br/>'; 
//                finances_a[i]['Month']= tmp[0];
//                finances_a[i]['Year']= tmp[1];
//                finances_a[i]['Value']= val[1];
            });
        
        


    </script>
</body>
</html>
 

Ordep-20

Active Coder
I updated the JS code

JavaScript:
      const finances = [ ['Jan-2010', 867884], ['Feb-2010', 984655], ['Mar-2010', 322013], ['Apr-2010', -69417], ['May-2010', 310503], ['Jun-2010', 522857], ['Jul-2010', 1033096], ['Aug-2010', 604885], ['Sep-2010', -216386], ['Oct-2010', 477532], ['Nov-2010', 893810], ['Dec-2010', -80353], ['Jan-2011', 779806], ['Feb-2011', -335203], ['Mar-2011', 697845], ['Apr-2011', 793163], ['May-2011', 485070], ['Jun-2011', 584122], ['Jul-2011', 62729], ['Aug-2011', 668179], ['Sep-2011', 899906], ['Oct-2011', 834719], ['Nov-2011', 132003], ['Dec-2011', 309978], ['Jan-2012', -755566], ['Feb-2012', 1170593], ['Mar-2012', 252788], ['Apr-2012', 1151518], ['May-2012', 817256], ['Jun-2012', 570757], ['Jul-2012', 506702], ['Aug-2012', -1022534], ['Sep-2012', 475062], ['Oct-2012', 779976], ['Nov-2012', 144175], ['Dec-2012', 542494], ['Jan-2013', 359333], ['Feb-2013', 321469], ['Mar-2013', 67780], ['Apr-2013', 471435], ['May-2013', 565603], ['Jun-2013', 872480], ['Jul-2013', 789480], ['Aug-2013', 999942], ['Sep-2013', -1196225], ['Oct-2013', 268997], ['Nov-2013', -687986], ['Dec-2013', 1150461], ['Jan-2014', 682458], ['Feb-2014', 617856], ['Mar-2014', 824098], ['Apr-2014', 581943], ['May-2014', 132864], ['Jun-2014', 448062], ['Jul-2014', 689161], ['Aug-2014', 800701], ['Sep-2014', 1166643], ['Oct-2014', 947333], ['Nov-2014', 578668], ['Dec-2014', 988505], ['Jan-2015', 1139715], ['Feb-2015', 1029471], ['Mar-2015', 687533], ['Apr-2015', -524626], ['May-2015', 158620], ['Jun-2015', 87795], ['Jul-2015', 423389], ['Aug-2015', 840723], ['Sep-2015', 568529], ['Oct-2015', 332067], ['Nov-2015', 989499], ['Dec-2015', 778237], ['Jan-2016', 650000], ['Feb-2016', -1100387], ['Mar-2016', -174946], ['Apr-2016', 757143], ['May-2016', 445709], ['Jun-2016', 712961], ['Jul-2016', -1163797], ['Aug-2016', 569899], ['Sep-2016', 768450], ['Oct-2016', 102685], ['Nov-2016', 795914], ['Dec-2016', 60988], ['Jan-2017', 138230], ['Feb-2017', 671099] ];
        
       let res = document.getElementById('result');   
       var finances_a={};
       let tmp;       
            let x=0;
            finances.forEach(function(val, i)
            {
                
                 //res.innerHTML +='<br/>'+val[0]+':'+val[1]+'<br/>'; 
                 tmp = val[0].split('-');
                 res.innerHTML +='<br/>'+tmp[0]+" "+tmp[1]+':'+val[1]+'<br/>'; 
                  finances_a[x]={'Month':tmp[0],'Year':tmp[1],'Value':val[1]} ; x++;
            });
        
        
            console.log(finances_a);

The var finances_a is the associative array


Captura de Tela 2022-11-14 às 16.08.21.png
 

cno

Coder
Ok first to uses sort functions you will need clean this values


JavaScript:
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style>
      * {
              box-sizing: border-box;
          }

          body {
              margin: 0px 130px;
              padding: 0;
              height: 100vh;
          }

          .container {
              width: 800px;
              margin: 0 auto;
          }

          #primary_header {
              display: flex;
              background-color: rgb(248, 109, 66);
              margin: 0px 0px;
              justify-content: center;
              font: bold;
          }

          #invoice {
              letter-spacing: 20px;
              color: white;
              font: Helvetica, Sans-Serif;
          }

          .logo {
              margin: 0px 5px;
          }

          .logo img {
              justify-content: center;
              height: 43px;
              width: 53px;
              margin: 10px;
              filter: invert(100%);
          }

          .secondary_header {
              display: flex;
              font-size: 1rem;
              background-color: rgb(233, 245, 250);
          }

          .info {
              margin: 30px 20px;
          }

          .info #c_name {
              font-weight: bold;
          }

          #meta {
              border-bottom: 1px solid black;
              margin: 0px 20px 0px 180px;
              width: 220px;
          }

          #meta td {
              text-align: right;
          }

          #meta td.meta-head {
              text-align: left;
              color: black;
              font: bold;
              font-size: 1rem;
          }

          .customer {
              background-color: rgb(233, 245, 250);
          }

          .customer p {
              margin: 0px 20px;
              padding-top: 30px;
              color: rgb(248, 109, 66);
          }

          #address {
              background-color: rgb(233, 245, 250);
              margin: 20px;
              padding: 10px 10px;
              width: 50%;
              height: 50px;
              font-size: 0.9rem;
              border-radius: 10px;
              resize: none;
              border: none;
          }

          #items {
              width: 80%;
              border-collapse: collapse;
              border-spacing: 0;
              border: 1px solid black;
          }

          #items td,
          th {
              border: 1px solid black;
          }

          #items {
              justify-content: center;
              width: 100%;
              margin: 0 0 0 0;
              font-family: Arial, Helvetica, sans-serif;
          }

          #items input,
          textarea {
              width: 120px;
              height: 30px;
          }

          table tr.item-row td {
              border: 1;
          }

          #items td.item-name {
              width: 140px;
          }

          textarea {
              resize: none;
          }

          .description {
              width: 300px;
          }

          #items textarea {
              margin-top: 4px;
          }

          #items .price {
              margin: 0px 9px;
              text-align: center;
          }

          #items td.description textarea,
          #items td.item-name input,
          #items td.cost input,
          #items td.qty input {
              width: 100%;
          }

          #items td.total-line {
              padding: 5px;
              border-right: 0;
              text-align: right;
          }

          #items td.total-value {
              border-left: 0;
              padding: 10px;
          }

          #items td.total-value {
              border-left: 0;
              padding: 10px;
          }

          #items td.balance {
              background: #eee;
          }

          #items td.blank {
              border: 0;
          }

          #terms {
              text-align: center;
              margin: 20px 0 0 0;
          }

          input:hover,
          input:focus,
          #items td.total-value input:hover,
          #items td.total-value input:focus,
          .delete:hover {
              background-color: #e2e2e2;
          }

          .success {
              background-color: #24b96f ;
          }

          .danger {
              background-color: #ff5722 ;
          }

          .action_container>* {
              border: none;
              outline: none;
              color: #fff;
              text-decoration: none;
              padding: 4px 10px;
              margin: 2px;
              cursor: pointer;
              transition: 0.3s ease-in-out;
          }

          .action_container>*:hover {
              filter: hue-rotate(-20deg) brightness(0.97);
              transform: scale(1.05);
              border-color: transparent;
              box-shadow: 0 1px 10px rgba(207, 207, 207, 0.267);
              border-radius: 2px;
          }

          .terms {
              text-align: center;
              text-transform: uppercase;
              font: 10px Helvetica, Sans-Serif;
              border-bottom: 1px solid black;
              padding: 8px;
              font-family: Verdana;
          }

          footer {
              height: 20px;
              width: 800px;
              margin: 0 auto;
              background-color: rgb(248, 109, 66);
          }



    </style>
<title>Ordened array</title>
    <p id='result'></p>
</head>

<body>
    <div class="container">
    </div>
    <footer id="footer "></footer>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js'></script>
    <script type="text/javascript">
      const finances = [ ['Jan-2010', 867884], ['Feb-2010', 984655], ['Mar-2010', 322013], ['Apr-2010', -69417], ['May-2010', 310503], ['Jun-2010', 522857], ['Jul-2010', 1033096], ['Aug-2010', 604885], ['Sep-2010', -216386], ['Oct-2010', 477532], ['Nov-2010', 893810], ['Dec-2010', -80353], ['Jan-2011', 779806], ['Feb-2011', -335203], ['Mar-2011', 697845], ['Apr-2011', 793163], ['May-2011', 485070], ['Jun-2011', 584122], ['Jul-2011', 62729], ['Aug-2011', 668179], ['Sep-2011', 899906], ['Oct-2011', 834719], ['Nov-2011', 132003], ['Dec-2011', 309978], ['Jan-2012', -755566], ['Feb-2012', 1170593], ['Mar-2012', 252788], ['Apr-2012', 1151518], ['May-2012', 817256], ['Jun-2012', 570757], ['Jul-2012', 506702], ['Aug-2012', -1022534], ['Sep-2012', 475062], ['Oct-2012', 779976], ['Nov-2012', 144175], ['Dec-2012', 542494], ['Jan-2013', 359333], ['Feb-2013', 321469], ['Mar-2013', 67780], ['Apr-2013', 471435], ['May-2013', 565603], ['Jun-2013', 872480], ['Jul-2013', 789480], ['Aug-2013', 999942], ['Sep-2013', -1196225], ['Oct-2013', 268997], ['Nov-2013', -687986], ['Dec-2013', 1150461], ['Jan-2014', 682458], ['Feb-2014', 617856], ['Mar-2014', 824098], ['Apr-2014', 581943], ['May-2014', 132864], ['Jun-2014', 448062], ['Jul-2014', 689161], ['Aug-2014', 800701], ['Sep-2014', 1166643], ['Oct-2014', 947333], ['Nov-2014', 578668], ['Dec-2014', 988505], ['Jan-2015', 1139715], ['Feb-2015', 1029471], ['Mar-2015', 687533], ['Apr-2015', -524626], ['May-2015', 158620], ['Jun-2015', 87795], ['Jul-2015', 423389], ['Aug-2015', 840723], ['Sep-2015', 568529], ['Oct-2015', 332067], ['Nov-2015', 989499], ['Dec-2015', 778237], ['Jan-2016', 650000], ['Feb-2016', -1100387], ['Mar-2016', -174946], ['Apr-2016', 757143], ['May-2016', 445709], ['Jun-2016', 712961], ['Jul-2016', -1163797], ['Aug-2016', 569899], ['Sep-2016', 768450], ['Oct-2016', 102685], ['Nov-2016', 795914], ['Dec-2016', 60988], ['Jan-2017', 138230], ['Feb-2017', 671099] ];
       
       let res = document.getElementById('result');  
       var finances_a={};
       let tmp;      
   
            finances.forEach(function(val, i)
            {
                 //res.innerHTML +='<br/>'+val[0]+':'+val[1]+'<br/>';
                 tmp = val[0].split('-');
                 res.innerHTML +='<br/>'+tmp[0]+" "+tmp[1]+':'+val[1]+'<br/>';
//                finances_a[i]['Month']= tmp[0];
//                finances_a[i]['Year']= tmp[1];
//                finances_a[i]['Value']= val[1];
            });
       
       


    </script>
</body>
</html>
I am using index.js
 

Ordep-20

Active Coder
I am using index.js
I put on html to see the exit, you can edit In your Index.js and use console.log(var) to see the exits


You can use this kind a function to order the dates example


JavaScript:
const input = [
  {
    Year: 2018,
    Month: 'Dec'
  },
  {
    Year: 2017,
    Month: 'Apr'
  },
  {
    Year: 2018,
    Month: 'Mar'
  },
  {
    Year: 2018,
    Month: 'Oct'
  },
  {
    Year: 2017,
    Month: 'Jan'
  },
  {
    Year: 2018,
    Month: 'Apr'
  }
];
const Months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
input.sort((a, b) => {
  if (a.Year !== b.Year) return a.Year - b.Year;
  return Months.indexOf(a.Month) - Months.indexOf(b.Month)
});
console.log(input);
 

cno

Coder
JavaScript:
      const finances = [ ['Jan-2010', 867884], ['Feb-2010', 984655], ['Mar-2010', 322013], ['Apr-2010', -69417], ['May-2010', 310503], ['Jun-2010', 522857], ['Jul-2010', 1033096], ['Aug-2010', 604885], ['Sep-2010', -216386], ['Oct-2010', 477532], ['Nov-2010', 893810], ['Dec-2010', -80353], ['Jan-2011', 779806], ['Feb-2011', -335203], ['Mar-2011', 697845], ['Apr-2011', 793163], ['May-2011', 485070], ['Jun-2011', 584122], ['Jul-2011', 62729], ['Aug-2011', 668179], ['Sep-2011', 899906], ['Oct-2011', 834719], ['Nov-2011', 132003], ['Dec-2011', 309978], ['Jan-2012', -755566], ['Feb-2012', 1170593], ['Mar-2012', 252788], ['Apr-2012', 1151518], ['May-2012', 817256], ['Jun-2012', 570757], ['Jul-2012', 506702], ['Aug-2012', -1022534], ['Sep-2012', 475062], ['Oct-2012', 779976], ['Nov-2012', 144175], ['Dec-2012', 542494], ['Jan-2013', 359333], ['Feb-2013', 321469], ['Mar-2013', 67780], ['Apr-2013', 471435], ['May-2013', 565603], ['Jun-2013', 872480], ['Jul-2013', 789480], ['Aug-2013', 999942], ['Sep-2013', -1196225], ['Oct-2013', 268997], ['Nov-2013', -687986], ['Dec-2013', 1150461], ['Jan-2014', 682458], ['Feb-2014', 617856], ['Mar-2014', 824098], ['Apr-2014', 581943], ['May-2014', 132864], ['Jun-2014', 448062], ['Jul-2014', 689161], ['Aug-2014', 800701], ['Sep-2014', 1166643], ['Oct-2014', 947333], ['Nov-2014', 578668], ['Dec-2014', 988505], ['Jan-2015', 1139715], ['Feb-2015', 1029471], ['Mar-2015', 687533], ['Apr-2015', -524626], ['May-2015', 158620], ['Jun-2015', 87795], ['Jul-2015', 423389], ['Aug-2015', 840723], ['Sep-2015', 568529], ['Oct-2015', 332067], ['Nov-2015', 989499], ['Dec-2015', 778237], ['Jan-2016', 650000], ['Feb-2016', -1100387], ['Mar-2016', -174946], ['Apr-2016', 757143], ['May-2016', 445709], ['Jun-2016', 712961], ['Jul-2016', -1163797], ['Aug-2016', 569899], ['Sep-2016', 768450], ['Oct-2016', 102685], ['Nov-2016', 795914], ['Dec-2016', 60988], ['Jan-2017', 138230], ['Feb-2017', 671099] ];
       
       let res = document.getElementById('result');  
       var finances_a={};
       let tmp;      
            let x=0;
            finances.forEach(function(val, i)
            {
               
                 //res.innerHTML +='<br/>'+val[0]+':'+val[1]+'<br/>';
                 tmp = val[0].split('-');
                 res.innerHTML +='<br/>'+tmp[0]+" "+tmp[1]+':'+val[1]+'<br/>';
                  finances_a[x]={'Month':tmp[0],'Year':tmp[1],'Value':val[1]} ; x++;
            });
       
       
            console.log(finances_a);

The var finances_a is the associative array


View attachment 1751
Code:
console.log("Total months:", finances.length);
console.log
// Amount of profit over entire period

const financesArray = finances.map((el) => el[1]);
const total = financesArray.reduce(
    (accVal, curVal) => accVal + curVal,
    0);
console.log('Total : ', total);
//  average

This is printing question 1 and question 2 but I am having issues getting questions 3 "The average change from month to month " which should print :
  1. Average Change: 7803.48
 

cno

Coder
you have to separate the datas you need


If you are using the associative array you will have to edit the functions;; const financesArray = finances.map((el) => el[1]);
I have tried this:
JavaScript:
const total = finances.map(el => el[1]).reduce((a, b) => a + b);
const average = total / finances.length;
console.log("Total:", total);
console.log("Average per month:", average.toFixed(2));

but the number I am getting is wrong, I am missing something
 

OldMan

King Coder
A fast look at the above - I'm thinking it's pretty complex. Let's see if this is a little easier:
Make an array for the amounts and another for the months. Then fill them with the difference between months
Code:
let dates=[];
let amounts = [];
for (let i = 0; i < finances.length-1; i++) {
    amounts[i] = finances[i+1][1] - finances[i][1];
    dates[i] = finances[i+1][0];
}
We need to preserve our amount array
let amountsClone = amounts.map(i => i);
Now I'm going to sort the array - the first element will be the lowest value and the last element will be the highest
amounts.sort(function(a, b){return a - b});

Here's how to find the highest number and the month:
let x = amounts[amounts.length-1];
let y = amountsClone.indexOf(x);

Alltogether
Code:
<script>
        
        var finances = [
            ["Jan-2010", 867884],
            ["Feb-2010", 984655],
            ["Mar-2010", 322013],
            ["Apr-2010", -69417],
            ["May-2010", 310503],
            ["Jun-2010", 522857],
            ["Jul-2010", 1033096],
            ["Aug-2010", 604885],
            ["Sep-2010", -216386],
            ["Oct-2010", 477532],
            ["Nov-2010", 893810],
            ["Dec-2010", -80353],
            ["Jan-2011", 779806],
            ["Feb-2011", -335203],
            ["Mar-2011", 697845],
            ["Apr-2011", 793163],
            ["May-2011", 485070],
            ["Jun-2011", 584122],
            ["Jul-2011", 62729],
            ["Aug-2011", 668179],
            ["Sep-2011", 899906],
            ["Oct-2011", 834719],
            ["Nov-2011", 132003],
            ["Dec-2011", 309978],
            ["Jan-2012", -755566],
            ["Feb-2012", 1170593],
            ["Mar-2012", 252788],
            ["Apr-2012", 1151518],
            ["May-2012", 817256],
            ["Jun-2012", 570757],
            ["Jul-2012", 506702],
            ["Aug-2012", -1022534],
            ["Sep-2012", 475062],
            ["Oct-2012", 779976],
            ["Nov-2012", 144175],
            ["Dec-2012", 542494],
            ["Jan-2013", 359333],
            ["Feb-2013", 321469],
            ["Mar-2013", 67780],
            ["Apr-2013", 471435],
            ["May-2013", 565603],
            ["Jun-2013", 872480],
            ["Jul-2013", 789480],
            ["Aug-2013", 999942],
            ["Sep-2013", -1196225],
            ["Oct-2013", 268997],
            ["Nov-2013", -687986],
            ["Dec-2013", 1150461],
            ["Jan-2014", 682458],
            ["Feb-2014", 617856],
            ["Mar-2014", 824098],
            ["Apr-2014", 581943],
            ["May-2014", 132864],
            ["Jun-2014", 448062],
            ["Jul-2014", 689161],
            ["Aug-2014", 800701],
            ["Sep-2014", 1166643],
            ["Oct-2014", 947333],
            ["Nov-2014", 578668],
            ["Dec-2014", 988505],
            ["Jan-2015", 1139715],
            ["Feb-2015", 1029471],
            ["Mar-2015", 687533],
            ["Apr-2015", -524626],
            ["May-2015", 158620],
            ["Jun-2015", 87795],
            ["Jul-2015", 423389],
            ["Aug-2015", 840723],
            ["Sep-2015", 568529],
            ["Oct-2015", 332067],
            ["Nov-2015", 989499],
            ["Dec-2015", 778237],
            ["Jan-2016", 650000],
            ["Feb-2016", -1100387],
            ["Mar-2016", -174946],
            ["Apr-2016", 757143],
            ["May-2016", 445709],
            ["Jun-2016", 712961],
            ["Jul-2016", -1163797],
            ["Aug-2016", 569899],
            ["Sep-2016", 768450],
            ["Oct-2016", 102685],
            ["Nov-2016", 795914],
            ["Dec-2016", 60988],
            ["Jan-2017", 138230],
            ["Feb-2017", 671099]
        ];
        let dates=[];
        let amounts = [];
        for (let i = 0; i < finances.length-1; i++) {
            amounts[i] = finances[i+1][1] - finances[i][1];
            dates[i] = finances[i+1][0];
        }
        let amountsClone = amounts.map(i => i);
        amounts.sort(function(a, b){return a - b});
        let x = amounts[amounts.length-1];
        let y = amountsClone.indexOf(x);
        alert(dates[y]);
    </script>
 

Ordep-20

Active Coder
The steps I made before will give you an associative array with month year and value, your actual data its not suitable for what you wanto to use cause you have concat between Month and Year, you need separate, re ordenate by month and year before you calculate the values, but keep in mind your first step on calculations will be by Year, or use the acumulative for looses and gains by year, examples

Calculations
2011
Jan - Dec :+70000
Jan - Dec : -15000 -> this data will form another array
Jan - Dec : 55000

2012

Jan - Dec : 250000
Jan - Dec : - 300000 -> this data will form another array
Jan - Dec -5000

In parallel you can use a function to calculate the acumulative of years the the result you multiply to 12 example

Jan -2011

to

Jan - 2022 -> accumulative 11 years -> 9 * 12 = 132 months
 
Last edited:
Top