Anton Bollen Forsberg
Anton Bollen Forsberg

Reputation: 45

Calculate percent change over multiple dynamic values with Javascript

I have a table displaying values for each month over a year. It looks something like:

Month |  2015  |
----------------
Jan   |  4856  |
Feb   |  6521  |
Mar   |  ....  |
Apr   |  ....  |
May   |  ....  |
Jun   |  ....  |
Jul   |  ....  |
Aug   |  ....  |
Sep   |  ....  |
Oct   |  ....  |
Nov   |  ....  |
Dec   |  ....  |
----------------
SUM   |  48956 |

I have an array collecting all the SUM values like this:

sum = [48956]

The user can then choose to add more years of values to the table like:

Month |  2013  |  2014  |  2015  |
----------------------------------
Jan   |  6587  |  8954  |  4856  |
Feb   |  1254  |  1125  |  6521  |
Mar   |  ....  |  ....  |  ....  |
Apr   |  ....  |  ....  |  ....  |
May   |  ....  |  ....  |  ....  |
Jun   |  ....  |  ....  |  ....  |
Jul   |  ....  |  ....  |  ....  |
Aug   |  ....  |  ....  |  ....  |
Sep   |  ....  |  ....  |  ....  |
Oct   |  ....  |  ....  |  ....  |
Nov   |  ....  |  ....  |  ....  |
Dec   |  ....  |  ....  |  ....  |
----------------------------------
SUM   |  35780 |  96448 |  48956 |
----------------------------------
diff  |        | %change| %change|

The array now looks like:

sum = [35780, 96448, 48956]

Now i want the "diff" row to show the increase or decrease in percentage over the years.
2014 compared with 2013, 2015 compared with 2014 and so on..

How can i grab ex. 96448 and 35780 in the array and calculate the diff value for 2014, and then the same for each new year added to the table?

96448 - 35780 = 60668 / 96448 = 0,629 * 100 = 62,9% 

Ofcourse the first year (2013 in this case) has no diff.

Upvotes: 3

Views: 3524

Answers (4)

malifa
malifa

Reputation: 8165

const values = [10000, 20000, 25000, 5000]; // values per year sorted ascending

const increases = values.map((currVal, index) => {
    if (index === 0) {
        return;
    }

    const prevVal = values[index - 1];
    return ((currVal - prevVal) / prevVal) * 100;
}).filter(Boolean);

console.log(increases); // [100, 25, -80] values in percent

This will return a list with your increase (%) from year to year.

Upvotes: 2

Moritz
Moritz

Reputation: 471

To just calculate the changes you can do something like this:

let sums = [35780, 96448, 48956];
let changes = [];
for (let i = 1; i < sums.length; i++) {
    changes[i] = sums[i] / sums[i-1] * 100; 
}

Upvotes: 0

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

The solution using Array.reduce function:

var sum = [35780, 96448, 48956],
    diff = ['-'];  // first column is empty '-'

sum.reduce(function (a, b) {
    if (a === 0) return b;
    var isNegative = b - a < 0;
    diff.push(parseInt((b - a) / (isNegative? a : b) * 100));
    return b;
}, 0);

console.log(diff);  // ["-", 62, -49]

Upvotes: 1

Nadeemmnn Mohd
Nadeemmnn Mohd

Reputation: 724

jsFiddle

Code using jquery

 var arr=[48956, 96448, 35780];
        //var arr=[96448, 35780];         
          var diff=[];
          $.each(arr,function(i,v){
           //96448 - 35780 = 60668 / 96448 = 0,629 * 100 = 62,9%
           if(i==arr.length-1){
            diff.push('---');
            return;
           }
           var first=v;
           var second=arr[i+1];
             var calc =(((first - second)/first)*100).toFixed(2)+'%';
             diff.push(calc);
          });
          alert(diff.toString());

code using Javascript

 var arr=[48956, 96448, 35780];
        //var arr=[96448, 35780];         
          var diff=[];
          for(var i=0;i<arr.length;i++)
      {
           //96448 - 35780 = 60668 / 96448 = 0,629 * 100 = 62,9%
           if(i==arr.length-1){
            diff.push('---');
            }else{
            var first=arr[i];
            var second=arr[i+1];
            var calc =(((first - second)/first)*100).toFixed(2)+'%';
            diff.push(calc);
            }
          }
          alert(diff.toString());

JSFiddle using javascript

Upvotes: 0

Related Questions