PDA

View Full Version : Resolved New to Javascript need Equation advice

JBerm
06-01-2009, 11:45 PM
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.

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
}
}

Now the problem I am having is in the else statement with the equation not executing correctly and giving a value of "0" since I am rounding. With the current equation and with a "Rating" of 1600 the "returnValue" should equal 465, but that is never the case. If I use Math.pow:

{
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
}

The result is 1.7133353971544615e+27 when again it should be 465. I know I'm missing something simple but with my lack of knowledge of javascript I'm just not seeing it.

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.

JB

venegal
06-02-2009, 12:15 AM
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:

returnValue = Math.round(1511.26 / (1 + 1639.28 * Math.pow(2.71828, (-0.00412 * Rating))));

Oh, and you're missing a right paren on all those else equations.

Old Pedant
06-02-2009, 12:20 AM
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?

Shannon Blonk
06-02-2009, 12:25 AM
The js code doesn't match the VB -- misinterpretation of precedence and/or misplaced parenthesis. Try
Math.round( 1511.26 / ( 1+ 1639.28 * Math.exp( -0.00412 * n)))

Too slow...

Old Pedant
06-02-2009, 12:38 AM
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:

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

And, yes, for a Rating of 1600, Points5 is then 465.

Sure hope this was NOT homework.

JBerm
06-02-2009, 02:08 AM
LOL! So this is homework you are both working on???

Sure hope this was NOT homework.

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:

returnValue = Math.round(1511.26 / (1 + 1639.28 * Math.pow(2.71828, (-0.00412 * Rating))));

Oh, and you're missing a right paren on all those else equations.

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 :D

Old Pedant
06-02-2009, 05:38 AM
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.