Javascript Return 0 Instead of NaN

May 11, 2022

So I'm having a bit of an issue with this calculator simulation I'm building. 

I've got it working with some Javascript for the most part until someone deletes a value from a number entry field. When they do, I think the Javascript is setting the variable to NaN and is basically breaking the whole thing. I want it to set the variable to zero instead. I tried a few different methods i found online and none of them seem to be working. 

Here's the code when it works (apart from the NaN issue)

var player = GetPlayer();
var income2021 = player.GetVar("BaseTaxYear2021Number");
var income2020 = player.GetVar("BaseTaxYear2020Number");
var ytdEarnings = player.GetVar("YTDCalc_YTDearnings");
var partialYear = player.GetVar("PartialYear");
var slVar_sum = (ytdEarnings + income2021 + income2020) / (24 + partialYear) 
var player = GetPlayer();
player.SetVar("Hourly_YTD2W2Avg",slVar_sum);
var monthly2020 = income2020 / 12
player.SetVar("Hourly_Income2020",monthly2020);

I tried adding in a few different methods of converting NaN to 0 that I found here, https://www.geeksforgeeks.org/how-to-convert-nan-to-0-using-javascript/
None of them seemed to work. Is storyline's Javascript just not equipped for using this function or am I maybe coding it in wrong?

Be the first to reply