- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-18-2019 10:28 AM
Could anyone please tell me why parseInt() is not working?
for (var i = 0; i <= 10; i++) {
var a = '0' + i;
var b = parseInt(a);
gs.log('a = ' + a + '; b = ' + b);
}
The output is:
*** Script: a = 00; b = 0
*** Script: a = 01; b = 1
*** Script: a = 02; b = 2
*** Script: a = 03; b = 3
*** Script: a = 04; b = 4
*** Script: a = 05; b = 5
*** Script: a = 06; b = 6
*** Script: a = 07; b = 7
*** Script: a = 08; b = NaN
*** Script: a = 09; b = NaN
*** Script: a = 010; b = 8
Thanks,
Ryan
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-18-2019 10:41 AM
Looks like it is octal, and you have to specify base 10:
for (var i = 0; i <= 20; i++) {
var a = '0' + i;
var b = parseInt(a,10);
gs.log('a = ' + a + '; b = ' + b);
gs.log(typeof a);
gs.log(typeof b);
}
gs.log(parseInt('10'));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-18-2019 11:13 AM
Thank you, Mike & Prateek!
Issue: this is a ServiceNow bug due to using a non-standard, out-of-date version of JavaScript, circa the year 2011.
In order to run correctly, the code needs to read:
var base = 10;
for (var i = 0; i <= 10; i++) {
var a = '0' + i;
var b = parseInt(a, base);
gs.log('a = ' + a + '; b = ' + b);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-18-2019 11:18 AM
Yep, Could you please mark the appropriate response as correct and close this thread.
Please mark my response as correct and helpful if it helped solved your question.
-Thanks