Hello everyone!
Something is bothering for hours now and I can’t figure out what’s wrong. As wix code doesn’t have the pow() function I assumed we could use " ^ " but it seems like it doesn’t work…
I’m trying to make a basique tool to transform annual rates into monthly rates. Here is a piece of the code, if someone has a clue on how to do that…
//growth from year n to n+1
var Aug1
//current year’s profit
var Rn
//next year’s profit
var Rn1
…
Rn=$w(‘#input6’).value
Rn1=$w(‘#input7’).value
Aug1=(Rn1/Rn-1)*100
…
$w(‘#slider15’).value=Aug1
//monthly growth needed to get the second year’s profit
$w(‘#slider17’).value= ((1+Aug1/100)^(1/12))-1
Many thanks in advance !
PS : currently for a growth of 100% in 12 months it says it’s 1% per month…
I’m guessing it’s doing integer math; does replacing the following line:
$w('#slider17').value= ((1+Aug1/100)^(1/12))-1
with the following line:
$w('#slider17').value = ((1 + parseFloat(Aug1 / 100)) ^ parseFloat(1 / 12)) - 1
make it work for you?
Hey!
First of all thank’s a lot for your answer!
But it doesn’t work, the result still has nothing to do with the reality…
@Paul Grillet - I think @abergquist is on the right track but I think applied the solution to the wrong statements 
I think you need to check the types you have set your input values to. Are they Numbers or are they Text? Text will not allow you to do math the way you are expecting.
You can check in the console when in preview mode by adding this below your assignments to Rn and Rn1.
Rn=$w('#input6').value
Rn1=$w('#input7').value
console.log("Rn type is: "+typeof Rn);
console.log("Rn type is: "+typeof Rn1);
If these log statements tell you that you are using Strings then you can use the parseFloat() function to force the values to be floatingPoint (see below) OR you can change the types in the Editor. Also note that if an Integer is added in the input field then your assignment may still need parsed to a float. Experiment and see what happens!
Rn= parseFloat($w('#input6').value)
Rn1= parseFloat($w('#input7').value)
console.log("Rn type is: "+typeof Rn);
console.log("Rn type is: "+typeof Rn1);
If you are using numbers something else may be happening. If you share a link to the page you are having problems with it will be easier to give you some help.
Steve
HI Steve!
I checked and all the inputs are numbers, however I tried your method and I did not work either. I tried to break down every part of the process to see where the issue is and it seems like the exponent is the problem…
I tried with the following :
Rn = 100
Rn1 = 200
// expected results: growth = 100% and monthly growth = 5.9%
A = (1 + (Aug1/100))
//expected result: 2, it’s ok
B = A^(1/12)
//expected result: 1.059 but the result is 2 & same result it I try with A^(0.0833333)
C = (B-1)*100
//as B doesn’t work C can’t work either
If you want to try yourself here is the link, it’s in french but I translated the important parts in the red square so it’s easier for you : https://www.qualicontent.com/calcul-d-objectifs
Alright I figured it out, in case someone has the same issue, we just need to use ’ ** ’ instead of '^ '!
So many hours of my life wasted haha
Thank’s Steve Cropper and abergquist for your help !
Hi Paul: Thanks for the note.
Yes the answer is to be familiar with the Javascript built in arithmetic operands and Math functions:
Thanks, Paul.
So it sounds like the following works for you:
$w('#slider17').value = ((1 + (Aug1 / 100)) ** (1 / 12)) - 1
If that’s the case, then the following should also work for you:
$w('#slider17').value = Math.pow(1 + (Aug1 / 100), 1 / 12) - 1
(The latter solution involves fewer parentheses, too.)