Convert Text variable to number

I need to convert the value of a text variable from a Text Entry (variable name: KeyedText) to number, pass it to a number variable (name: KeyedResult) so the latter can be compared with a calculated value (Articulate Storyline doesn't allow direct comparison of a Text and a Number variable). With some research I used the following Javascript:

var player=GetPlayer();
var text = player.GetVar("KeyedText");
var number = integer.parse(text);
player.SetVar("KeyedResult", number);

The trigger was set as: 

When Text Entry loses focus
- Set KeyedText equal to the typed value
- Execute the JavaScript

However, after the execution of the JS, the value of the KeyedResult variable never changes from 0. Anyone can help troubleshoot? Thanks a lot.

5 Replies
Walt Hamilton

In this case, the js may grab the value of KeyedText before it is set, or KeyedText may be something that can't be parsed to an integer. Just for the purpose of testing hypothesis #1, call the js with the click of another button, to check if it is a timing problem.

As to the second hypothesis, I would be very nervous about depending on a learner to be guaranteed to enter digits in a text variable; I'd feel safer with a numeric variable.

If these wild guesses don't help you, attach the .story file here, and someone can take a closer look.