Complex Math with User entered variables

Apr 07, 2022

Hello e-Learning Heros,

I'm attempting to build a simulator in Storyline. It will simulate some complex math calculations which determine the end of year rating for a performance review.  The user entered variables are populating okay, but at some point the Javascript goes south.  I've narrowed it down to the player.GetVar area but not sure how to fix it.  I've pasted the java script below, and I've published the simulator to Review360 here: Performance Rating Simulator | Review 360 (articulate.com).

Any advice?

 

JavaScript:

//Bring variables in.

var player = GetPlayer();
var c1 = player.GetVar("Company1Rating");
var c2 = player.GetVar("Company2Rating");
var c3 = player.GetVar("Company3Rating");
var p1 = player.GetVar("Personal1Rating");
var p2 = player.GetVar("Personal2Rating");
var p3 = player.GetVar("Personal3Rating");
var v1 = player.GetVar("Values1Rating");
var v2 = player.GetVar("Values2Rating");
var v3 = player.GetVar("Values3Rating");
var companyNumberj = player.GetVar("CompanyNumber");
var personalNumberj = player.GetVar("PersonalNumber");

//Calculate Numeric Rating.

var numericRatingj = (((((c1 + c2 + c3) / companyNumberj) * 0.9) + (((p1 + p2 + p3) / personalNumberj) * 0.1) ) * .8) + (((v1 + v2 + v3) / 3) * 0.2);

//Export Numeric Rating.

player.SetVar("NumericRating",numericRatingj);

6 Replies
Sarah Hodge

Hi Katherine and welcome to E-Learning Heroes! 🙂 What a cool example! Although we don't provide support for JavaScript coding, I hope the community has some ideas they can share. The only advice I have is to perhaps select colors with more contrast for the text and background of the data fields so it's easier to read. I love using this color contrast checker to find the right combo. 

Excited to see how this turns out! 

Katherine Henderson

Thanks Math!  The example I've been working with uses 2 company goals, each with a rating of 2. This should return a value of 1.8, but it's returning weird ones like 72 or 165.  The error seems to be from the conversion of the variables from Storyline into Javascript, when I pull the values over to c1 and c2. If I enter the 2's directly into the javascript, the correct number is returned.  

So I suppose I'm looking for input from anyone failiar with the player.GetVar method?  Thanks for any insight you can provide!

 

Math Notermans

With variables in Storyline...especially when combining them with Javascript you have to be aware of a few things.

Always doublecheck you get numbers and not strings when getting a variable.
As Storyline tends to convert variables internally to strings, make sure before you do a calculation the value you are calculating with is indeed a Number.

So do use parseInt( ) to convert a variable to a Number.. or parseFloat( ) if you are expecting decimals. Also do read up on decimals in Storyline. Quite a lot of posts on it. Storyline internally converts all calculations to max 2 decimals. In fact it does calculate internally with 7 digits but somewhere along the line the endresult is set to max 2 digits.

As said... a lot to doublecheck and test and without a sample it is just a lot of work.