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

Alikutty A
Tera Sage

Hi,

Please try this out and see, assuming you have calculated the average

 

var num = '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';
var avg = 89.44;
var numArr = [];
var numbers = num.split(",");
for(var i=0; i<numbers.length; i++){
  var value = parseInt(numbers[i])-avg;
  numArr.push(value);
}
var finalStr = numArr.join(",");
alert(finalStr);

Thanks

Alikutty A
Tera Sage

Here is the entire code, I have tested it.

var sum = 0;
var numArr = [];
var num = '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';

var numbers = num.split(",");
for(var i=0; i<numbers.length; i++){
sum += parseInt(numbers[i]);
}

var avg = sum/25;

for(var i=0; i<numbers.length; i++){
  var value = parseInt(numbers[i])-avg;
  numArr.push(value);
}
var finalStr = numArr.join(",");
alert(finalStr);

Hi Alikutty,

Your code is working fine, but when I am adding into my code it is not running.

could you please execute the script in background script.. I am able to get the total and average.

But here is my actual code..  Now we have to Minus the Average from the  "str.charCodeAt(i) " ... here will be series of numbers..

 

 

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

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(","));