# 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

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.

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?

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,
Math

Thank you so much for your kind help, Math. It's greatly appreciated and you've put a smile on my face today. :-)

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. :-)

You may have to redesign your calculator as storyline is doing the rounding automatically. You could use a string instead to hold the value which would not get rounded and then adjust your javascript to account for this.

Hi Phil! Ugh. I have no idea how to do that.

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.

Ah, I see. Thank you both, as always, for your kind and gracious responses. I'll muddle through! :-)