PDA

View Full Version : Coding for Calculating Exponents?

UltraMegaOK1988
07-30-2007, 08:02 AM
I'm trying to create a little calculator for myself that will calculate a baseball team's expected win-loss record.

The formula is (Runs Scored per game + Runs Allowed per game) ^ .287

Since I don't have their runs scored/allowed per game, but I do have their total runs scored/allowed, I'm just going to add those together and divide it by the number of games they've played.

If you don't understand baseball, that probably doesn't make much sense, but I feel I'm pretty close to having this working, and I feel I know where my errors lie, but I'm not sure how to correct them.

Here's what I have:

<html>

SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function solvepy(form) {
a = parseInt(form.a.value);
b = parseInt(form.b.value);
d = parseInt(form.d.value);
form.c.value = Math.pow(((a+b)/d), .287)
}
// End -->
</script>

<body>
<a href="http://www.hardballtimes.com/main/statpages/glossary/#pyth" target="newwin">PythagenPat</a><br><br>
(Runs Scored per Game + Runs Allowed per Game) ^ .287<br><br>

In short, (RS/G+RA/G)^.287<br><br>

<form>
<input type=text name=a size=5> - Total Runs Scored<br>
<input type=text name=b size=5> - Total Runs Allowed<br>
<input type=text name=d size=5> - Total Games Played
<br><br>
<input type=button value="Calculate PWL" onClick="solvepy(this.form)">
<br><br>
<input type=text name=c size=5> - PWL

</form>

</body>

</html>

The bolded part is where I think my problems lie.

Thanks for any help that you can provide.

codeexploiter
07-30-2007, 09:29 AM
You haven't mentioned whether you get any error or you are looking for a checking of your output.

If you look at the source code you've provided

SCRIPT LANGUAGE="JavaScript">
You missed one "<" which must be

<SCRIPT LANGUAGE="JavaScript">
or

<script type="text/javascript">

I don't know baseball :D

UltraMegaOK1988
07-30-2007, 10:16 AM
I just forgot to include that bracket when I was copying. It's there in my file.

Everything works, except I don't think it's calculating correctly, which is why I think the error is with the part I bolded.

I'm not sure if I should include all those parentheses, or if I can Math.pow multiple variables (like a+b/d), etc.

I have no clue about JavaScript... I just wanted to make this for my own convenience.

Thanks.:D

EDIT: Well, I got it... but for a different calculation. I did a different Pythagorean formula for calculating a baseball team's expected win-loss record, and here's how I did it... hopefully I can learn from it, and maybe some other people will, too.

The problem was in the part I bolded above.

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function solvepy(form) {
a = parseInt(form.a.value);
b = parseInt(form.b.value);
form.c.value = (Math.pow(a,1.83)/(Math.pow(a,1.83)+Math.pow(b,1.83)))
}
// End -->
</script>