Add square and square root buttons to an existing Javascript calculator built in SL 360

Oct 16, 2021

Hello! A few years ago I obtained a wonderful calculator from a fellow user on this forum. I now need to add a square button and square route button. I am not fluent with Javascript. I have attached my calculator SL file. I believe it is quite simple and the javascript that is already there just needs to be copied to the two new buttons and tweaked with the new formulas. Might someone be able to help me, by chance? I would be so grateful.

9 Replies
Math Notermans

Indeed not to difficult. The square math is already there. Only thing needed was to set the type of calculation on the square button to 's'.
player.SetVar("calculation", "s");

The math for square root wasnot yet present in your code, but luckily Javascript has its own math functionality so using 'Math.sqrt' was easy enough.
mysum = Math.sqrt(mysum);

Setting the calculation to 'r' on the square root button...
player.SetVar("calculation", "r");

As the actual calculation just happens on the '='-button inthere another change needed.
} else if (mycalc == "r") {
mysum = Math.sqrt(mysum);

And thats it. Adding the fixed sample.


Shannon Connor

Oh, thank you so much! The square root works perfectly. Square, however, does not. If I want the square root of 4, for example, which should be 16, it gives me 8. Instead of multiplying the number times itself (4x4), it is multiplying it times 2. I've tried to fix, but alas, I apparently am not quite grasping how to do so. Might I trouble you for your kindness a second time? Note that the x2 button was copied from another button, so the "s" is already in use. Perhaps using a "q" and the math.pow method might work? 

Math Notermans

Ah yes, my mistake on the square function. Right...
Change indeed the var calculation to 'q'

player.SetVar("calculation", "q");

And then on the = use Math.pow when calculation = q
As Math.pow uses 2 input values Math.pow(2,2) you can add a variable to use as the power number. Im not sure how that works in regular calculators but in some way you should be able to calculate the power of 2,3 etc. etc.

Adding power as a variable in Storyline. Set to 2 for now.
And then i added:
var mypower = player.GetVar("power");

} else if (mycalc == "q") {
mysum = Math.pow(mysum, mypower);

And here it is complete.

If you want to change the power of 2 to the power of 3,4 or whatever... change the variable power in Storyline and that works too.

Kind regards,

Shannon Connor

Hi Math! Might you be able to answer another question in this same calculator? It's currently rounding to two digits. The client does not want rounding. He wants to enter 0.666 and not have it round to 0.67. He also wants to enter 1.58111. I have uploaded the latest file in the hopes you may have mercy on me. :-)

Math Notermans

Storyline Numeric Input always get rounded to two digits. So you have to replace all Numeric Input fields by Text Input fields.... or trick Storyline in multiplying by 1000 or 10.000 or so... then dividing it ( as Javascript doesnot have the 2 digit limitation... ) and then send that value as a String to a field showing it.... I dont have no time now to fix it for you alas.