- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2018 12:46 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2018 02:54 AM
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(","));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2018 12:53 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2018 12:58 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2018 02:43 AM
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 );
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2018 02:54 AM
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(","));