Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Help with for loop

harishdasari
Tera Guru

Hi,

I am writing a script, where it will have numbers something like this below

var string = 69,110,103,108,101,119,111,111,100,67,111,108,111,114,97,100,111,56,48,49,49,50,85,83,65;

SUM = 2236;

Length of the above numbers are 25

If we divide sum with length 2236/25 = 89.44 this is the average I am getting.

Now requirement is I want to subtract(minus) average from each of the number.

for example: if we minus 69-89.44 = -20.44, similarly 110-89.44 =20.56

I want to loop through all the numbers and minus with the average.

I am using for loop, but it is giving me some wrong results.

Thank you. 

1 ACCEPTED SOLUTION

Alikutty A
Tera Sage

Please try this out in background

var list = [];
var locationOnincidentForm = "Englewood Colorado 80112 USA";
var WH = locationOnincidentForm.replace(/-|\s/g,"");
var str = WH.replace(/\s/g,'');
var total= 0;
var numArr = [];
for (var i = 0; i < str.length; i++) 
{
total  += str.charCodeAt(i);
var average = total/str.length;
}	

gs.print('Total   ' + total);
gs.print('Average ' + average );
gs.print(str);

for (var i = 0; i < str.length; i++) 
{
var minus = str.charCodeAt(i)-average;
gs.print(minus);
list.push(minus);
}	

gs.print(list.join(","));

View solution in original post

5 REPLIES 5

Thank you so Much , it worked.