View Full Version : ot JavaScript help

06-21-2004, 01:58 AM
I'm trying to make a simple javascript so that I have 10 textforms.



5 more

I'm trying to make it so when you click a button, it checks to see if the sum of the #s you put in the top 5 forms equal the sum of the #s in the bottom 5 forms, and if so, it writes "Yes" in an 11th text form.

I've gotten it pretty far so that if you have something like this:

form1 says 2
form2 says 2


form1 says 2
form2 says 2

Click the button, it will say "Yes" in the 11th form.

However, it refuses to work if I did something like this:

form1 says 3
form2 says 1
form3 says 4


form1 says 3
form2 says 2
form3 says 3

If I have it like that, then click the button, nothing happens. It can't recognize that 2+3 = 1+4

How do I get it to recognize this?

06-21-2004, 03:05 AM
it might be doing a string comparison instead of the numeric comparison that you want it to. can you get it to add up the numbers in the text boxes? can you put the code which does the comparison?

06-21-2004, 12:30 PM
This is admittedly sloppy, I didn't care; just making it for myself ;)

<SCRIPT LANGUAGE = "JavaScript">
function check()
var player1a = document.players.p1.value;
var player2a = document.players.p2.value;
var player3a = document.players.p3.value;
var player4a = document.players.p4.value;
var player5a = document.players.p5.value;
var teama = player1a+player2a+player3a+player4a+player5a;
var player1b = document.players.pp1.value;
var player2b = document.players.pp2.value;
var player3b = document.players.pp3.value;
var player4b = document.players.pp4.value;
var player5b = document.players.pp5.value;
var teamb = player1b+player2b+player3b+player4b+player5b;
if (teama == teamb)
document.players.answer1.value = "Yes"
else document.players.answer1.value = "No"

06-21-2004, 04:38 PM
anyone? anyone? Bueller?

06-21-2004, 06:28 PM
You are adding strings so if player1=1, player2=2, player3=3 then
teama = 123.

You need to make it into a integer addition. One way to do this is:
teama=eval(player1a)+eval(player2a)+ etc.

06-21-2004, 06:40 PM
That worked great! Thanks!

Now, the next step is this:

What I'm basically trying to do it make up a simple "trade checker"

Besides the 11th form i mentioned above, I have 4 more after that.

I want it to be setup so like this:

Salaries Exactly Equal? <box>
Salaries Match Within +15% of Team 1? <box>
Salaries Match Within -15% of Team 1? <box>
Salaries Match Within +15% of Team 2? <box>
Salaries Match Within -15% of Team 2? <box>

Right now the first box will say yes or no if the #s add up to equal or not. So that's now done.

Now I need to figure out the correct way to tell the java to check if team1's sum is within the sum of team2 plus or minus 15% of team2's sum.

Meaning to check if team1's sum lies withing a range of team2's sum + or - 15% of team2's sum.2250

So if team2's sum is 15000, then i want to tell it to check if team1's sum lies within 12750 to 17250.

06-21-2004, 08:54 PM

So close.

06-22-2004, 11:18 AM
If you still don't have it working, this will tell you if teama is within 15% of teamb:

if ((teama >= .85 * teamb) && (teama <= 1.15 * teamb))

So if teamb = 100, teama must be 85 - 115. It is actually 85 - 114 since javascript does weird match when you multiply by decimals.

06-22-2004, 12:57 PM