JavaScript - calculate number values in a string
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2019 12:43 PM
Hello,
I am working on a script where I need a read an output and calculate the numbers at end of each line. I am trying the below script but I am unable make it further.
Note: script should be able to add values of 'Local Fixed disk' ONLY.
Output I get in the ecc queue:
Caption Description Size
C: Local Fixed Disk 64422408192
D: Local Fixed Disk 999645245440
G: CD-ROM Disc 99884729
Script to read and calculate the size
//I pasted the above output in the Incident short descripion to make script running easier.
var gr = new GlideRecord('incident');
gr.get('number','INC7914138');
var output= gr.description;
var lines = output.split('\n');
var diskSpace = 0;
for (var i = 0; i < lines.length; i++)
{
var newArray = lines[i];
if (newArray.indexOf('Local Fixed Disk'))
{
var n = newArray.split(" ");
var disk = (n[n.length - 1]);
diskSpace = diskSpace + parseInt(disk);
gs.print(diskSpace);
}
}
Expected output:
//addition
64422408192+999645245440 = 1064067653632

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2019 12:51 PM
Hi,
So what are you currently getting? We get what should be happening, but what are you currently getting? This helps with troubleshooting else we just speculate where your code went wrong, but the print could tell us a bit more.
Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2019 12:54 PM
Thanks for the reply Allen.
Output:
*** Script: NaN
*** Script: NaN
*** Script: NaN
*** Script: NaN
*** Script: NaN
*** Script: NaN
*** Script: NaN
*** Script: NaN

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2019 01:07 PM
I'd need to test if your split is truly splitting these values (could you try adding another print to see if you're even splitting correctly?)
But to get them in to a format you can add...try doing:
diskSpace = diskSpace + +disk;
This will at least if we're going in the right direction by adding 0 to that split string number.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2019 12:53 PM
Hi Paul,
I am noticing a couple potential problems. Could you show us what you are getting now, it helps with troubleshooting.
Thank you