Logic for array

Sengeni
Tera Contributor

hii,

I need a logic for array ,if we take 12 months values in array how can we calculate the average for each month. Please tell me a scripts for finding the average.

 

1 ACCEPTED SOLUTION

That's the full script. I've executed the script in Script Background to get the Execution result.

I've added comments in script.

var values = [[1,2,3], [4,5,6],[1,2,3], [4,5,6],[1,2,3], [4,5,6],[1,2,3], [4,5,6],[1,2,33], [4,53,6],[14,2,3], [41,15,6],];  // array of array to process

// prototype to sum array
Array.prototype.sum = Array.prototype.sum || function (){
  return this.reduce(function(p,c){return p+c},0); // sum values of elements in an array
};
// prototype to find average of an array
Array.prototype.avg = Array.prototype.avg || function () {
  return this.sum()/this.length; // find the sum of elements in an array and divide by number of elements
};

var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; // this is just to output name of months

for (var i=0; i< values.length; i++) { // loop through array of array "values"
  gs.info(months[i] + ' average is ' + values[i].avg()); // output name of month and average. values[i] is an array item in "values". Calling "avg" prototype defined above.
}

View solution in original post

9 REPLIES 9

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Sengeni,

How is the array structured? 

Something like below with first month being the first subarray?

[[1,2,3],[2,3,4,].....];

Vishnu Prasad K
Giga Guru

Hi Sengeni,

 

You can use the below code logic. Please recheck the formatting.

 

var sum = '';

var arr = {10,11,12,......};

var arrEle = arr.split(',');

var arrLen = arr.length();

for (var i=0, i<arrLen , i++){

sum = arr(i)++ ;

}

var avg = sum/arrLen ;

return avg

 

Sengeni
Tera Contributor

hii,

Like jan=1,feb=2,,,,,,dec=12

the array have to first run for jan month {[1\1]}

then array have to run for both jan and feb{[1+2]\2}

then for 3 months {[1+2+3]\3} 

.

.

.

 

{[1+2+3+4+5+6+7+8+9+10+11+12]\12} 

for calculating average/median for 12 months this was the logic

I may not be understanding the question but something like below?

Array.prototype.sum = Array.prototype.sum || function (){
  return this.reduce(function(p,c){return p+c},0);
};
Array.prototype.avg = Array.prototype.avg || function () {
  return this.sum()/this.length; 
};

var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var values = [];
for (var i=0; i< 12; i++) {
  values.push(i+1);
  gs.info(months[i] + ' average is ' + values.avg());
}

Execution result:

*** Script: Jan average is 1
*** Script: Feb average is 1.5
*** Script: Mar average is 2
*** Script: Apr average is 2.5
*** Script: May average is 3
*** Script: Jun average is 3.5
*** Script: Jul average is 4
*** Script: Aug average is 4.5
*** Script: Sep average is 5
*** Script: Oct average is 5.5
*** Script: Nov average is 6
*** Script: Dec average is 6.5