Forum Discussion

BryanTregunna's avatar
BryanTregunna
Community Member
3 years ago

Javascript Math.pow

Hi any javascripters out there in this brilliant community.

I need the user to carry out some calculations. They enter a series of data, with the result being calculated for them. Everything goes well until the final calculation, which is to multiply one variable "NetFig" to the power of another variable called "NetPower".  This is where I need some javascript (Math.pow).

I have no idea how javascript works, but I am an expert at copying and pasting. So I am looking for the code to:

  • extract the two variables,
  • do the calc 
  • give me the result

Please!

  • Basically this is the script you need...

    // connect with Storyline Player
    let player = GetPlayer();

    //Get the variables NetFig and NetPower
    let nFig = player.GetVar("NetFig");
    let nPow = player.GetVar("NetPower");

    // Do the calculation and set a variable to it
    let endRst = Math.pow(nFig, nPow);

    // Set the Storyline variable to it, so it shows in Storyline
    player.SetVar("EndResult",endRst);

    A few caveats to watch when implementing it. The vars NetFig and NetPower are numbers in Storyline. I added numeric inputs for them. If your variables are Strings...well you have to convert them to Numbers first.

    Further is there the dreaded often despised internal functionality that Storyline ( still ! ) limits numeric values to 2 digits. Do try and input NetFig = 7 and NetPower = -2. You get 0.02 whereas it should be 0.02040816326530612. You can solve this by showing the endresults as Strings because internally both Storyline and Javascript do the calculation correct, but somehow numeric values get limited to 2 decimals when showing in Storyline.

    And here you can test it...
    https://360.articulate.com/review/content/d714fde1-7952-4530-8993-afc2c23e1b55/review

  • As said... now you can see the full glory of all decimals... but the lower field is a textString, so make sure when doing calculations with it to convert it back to a Number.

    https://360.articulate.com/review/content/d714fde1-7952-4530-8993-afc2c23e1b55/review
     Code now is like this:

    // connect with Storyline Player
    let player = GetPlayer();

    //Get the variables NetFig and NetPower
    let nFig = player.GetVar("NetFig");
    let nPow = player.GetVar("NetPower");

    // Do the calculation and set a variable to it
    let endRst = Math.pow(nFig, nPow);

    //convert this to a string
    let tmpString = endRst.toString();

    // And set a Storyline String as endResult so you get more then 2 decimals
    player.SetVar("EndResultStr",tmpString);

    // Set the Storyline variable to it, so it shows in Storyline
    player.SetVar("EndResult",endRst);