Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 7 of 7

06022009, 12:45 AM #1
 Join Date
 Jun 2009
 Posts
 2
 Thanks
 3
 Thanked 0 Times in 0 Posts
New to Javascript need Equation advice
Hello I am new to using Javascript and have been trying to incorporate this equation into my if statement but cannot get it to calculate properly no matter which method I use. Here is the function as I have it worked out presently.
Code:function Points() { Rating = document.Arena.Rating.value; { if (Rating <= 1500){ returnValue = Math.round((0.22 * Rating + 14) * 0.76) }; else { returnValue = Math.round(1511.26 / (1 + 1639.28 * 2.71828 ^ (0.00412 * Rating)) * 0.76) }; document.Arena.Points2.value = returnValue } { if (Rating <= 1500){ returnValue = Math.round((0.22 * Rating + 14) * 0.88) }; else { returnValue = Math.round(1511.26 / (1 + 1639.28 * 2.71828 ^ (0.00412 * Rating)) * 0.88) }; document.Arena.Points3.value = returnValue } { if (Rating <= 1500){ returnValue = Math.round(0.22 * Rating + 14) }; else { returnValue = Math.round(1511.26 / (1 + 1639.28 * 2.71828 ^(0.00412 * Rating)) }; document.Arena.Points5.value = returnValue } }
Code:{ if (Rating <= 1500){ returnValue = Math.round(0.22 * Rating + 14) }; else { returnValue = Math.round(1511.26 / (Math.pow((1 + 1639.28 * 2.71828),(0.00412 * Rating))) }; document.Arena.Points5.value = returnValue }
Also I should note that this current project is a javascript conversion of my VB project I had made. Where the exponential value of (0.00412 * Rating) is noted by the "^" and then executed as an exponential value. The result is the same in all of the else statements but for simplicity I singled out the last one as an example.
Thank You in advance for any help that can be given.
JBLast edited by JBerm; 06022009 at 03:22 AM.
06022009, 01:15 AM
#2
 Join Date
 Apr 2009
 Posts
 2,127
 Thanks
 1
 Thanked 426 Times in 424 Posts
The reason the first one returns 0 is not that you are rounding, but that "^" isn't the power operator in javascript but the bitwise XOR.
The second one does exactly what you tell it to do, so if you where expecting 465 but getting 10^27, your equation is just horribly wrong.
Please describe in words what you are trying to accomplish.
Edit: Oh, I didn't really compare those two versions before. You completely changed it up! Just change it to the way it was before, but using Math.pow:
Oh, and you're missing a right paren on all those else equations.Code:returnValue = Math.round(1511.26 / (1 + 1639.28 * Math.pow(2.71828, (0.00412 * Rating))));
Last edited by venegal; 06022009 at 01:23 AM.
Users who have thanked venegal for this post:
JBerm (06022009)
06022009, 01:20 AM
#3
You are putting your semicolons in the wrong places.
But I don't think that's your big problem.
Yes, you must use Math.pow( number, power )
When I use Math.pow(1 + 1639.28 * 2.71828, 0.00412 * Rating ) that results in a very very tiny number, and so the results are always just 1511.26.
Could you maybe show us the formula in *math* terms, instead of in code?
Or show the VB code?
Users who have thanked Old Pedant for this post:
JBerm (06022009)
06022009, 01:25 AM
#4
 Join Date
 Mar 2009
 Location
 Fabric Covered Box
 Posts
 69
 Thanks
 1
 Thanked 16 Times in 14 Posts
The js code doesn't match the VB  misinterpretation of precedence and/or misplaced parenthesis. TryToo slow...Code:Math.round( 1511.26 / ( 1+ 1639.28 * Math.exp( 0.00412 * n)))
Users who have thanked Shannon Blonk for this post:
JBerm (06022009)
06022009, 01:38 AM
#5
LOL! So this is homework you are both working on???
Or you just recognize the formula??
Nice catch, in any case, Shannon.
So simplifying the code a lot:
And, yes, for a Rating of 1600, Points5 is then 465.Code:<script> function Points() { var form = document.Arena; var Rating = form.Rating.value; var powerFactor = 1 + 1639.28 * Math.exp( 0.00412 * Rating ) var ratingFactor = 0.22 * Rating + 14; var lower = (Rating <= 1500) form.Points2.value = Math.round( lower ? ratingFactor * 0.76 : 1511.26 / (powerFactor * 0.76) ); form.Points3.value = Math.round( lower ? ratingFactor * 0.88 : 1511.26 / (powerFactor * 0.88) ); form.Points5.value = Math.round( lower ? ratingFactor : 1511.26 / powerFactor ); } </script> <form name="Arena"> <input name="Rating" onchange="Points()"> <hr> Points2: <input name="Points2"><br/> Points3: <input name="Points3"><br/> Points5: <input name="Points5"><br/> </form>
Sure hope this was NOT homework.
06022009, 03:08 AM
#6
 Join Date
 Jun 2009
 Posts
 2
 Thanks
 3
 Thanked 0 Times in 0 Posts
To Old Pedant, no this isn't homework but like I said earlier something I'm trying to convert to Javascript from VB to learn the coding better so I figured given I have made the program in VB it would help me to learn by doing something familiar.
To venegal, thank you for the simple solution that was my growing headache. I knew I had to use Math.pow() it was just the placement within the equation that was the big issue it seems and was my initial problem.
I would like to thank everyone that had responded to the thread and the great ideas presented. I really appreciate the feedback and the help.
Thank You again,
JB
06022009, 06:38 AM
#7
Math.pow() is *NOT* the right answer.
And if you used the ^ operator in VB, *that* was not the right answer.
Math.exp() in JavaScript is the equivalent of the EXP( ) function in VB, which is what you should have been using.
Mind you, it probably doesn't make any practical difference whether you use EXP(x) or 2.718281828 ^ x, but the former will run just a bit faster.
If you weren't aware of it, A^B is calculated as EXP( B * LOG(A) ). So when you do 2.71828 ^ x you are really doing EXP( x * LOG(2.71828) ) which is EXP( x * 1 ) and so is EXP(x). But you forced the computer to do any extra LOG calculation and an extra multiply.