Results 1 to 9 of 9

Thread: ot JavaScript help

  1. #1
    .
    Join Date
    Jun 2004
    Posts
    52,583

    Default ot JavaScript help

    I'm trying to make a simple javascript so that I have 10 textforms.

    5

    then

    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?

  2. #2
    Member skyfire's Avatar
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Age
    33
    Posts
    1,559

    Default Re: ot JavaScript help

    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?

  3. #3
    .
    Join Date
    Jun 2004
    Posts
    52,583

    Default Re: ot JavaScript help

    This is admittedly sloppy, I didn't care; just making it for myself

    Code:
    <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"
    }
    </script>

  4. #4
    .
    Join Date
    Jun 2004
    Posts
    52,583

    Default Re: ot JavaScript help

    anyone? anyone? Bueller?

  5. #5
    LAPacer
    Guest

    Default Re: ot JavaScript help

    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.

  6. #6
    .
    Join Date
    Jun 2004
    Posts
    52,583

    Default Re: ot JavaScript help

    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.

  7. #7
    .
    Join Date
    Jun 2004
    Posts
    52,583

    Default Re: ot JavaScript help

    :sadface:

    So close.

  8. #8
    LAPacer
    Guest

    Default Re: ot JavaScript help

    If you still don't have it working, this will tell you if teama is within 15% of teamb:

    Code:
    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.

  9. #9
    .
    Join Date
    Jun 2004
    Posts
    52,583

    Default Re: ot JavaScript help

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •