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();
var monthly2020 = income2020 / 12

I tried adding in a few different methods of converting NaN to 0 that I found here,
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