Convert Text variable to number
Feb 21, 2020
By
Pollux Chen
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
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.
I would run the javascript on when variable changes that way you know it has been set.
Check the javascript console.
I may be crazy though but the simple answer is to swap the text entry field for a number entry
I'm not familiar with integer.parse().
Instead, try using: var number = parseInt(text);
Thanks Russell, this way works!
Thanks Phil, I finally troubleshot by replacing the javascript function to parseInt(text)
This discussion is closed. You can start a new discussion or contact Articulate Support.