Link to home
Start Free TrialLog in
Avatar of hefterr
hefterrFlag for United States of America

asked on

Help with a small Javascript

Hi,
I am a newbie to JavaScript (REALLY).  I am trying to test/debug a small script to calculate ELO changes between to players in a game.  An ELO is a rating system.  It is an exchange of points between 2 players based on your probability of winning and the maximum amount of points possible (16 in this case).

It's hard for me to interpret some of this code.  It works properly in calculating the new ELO for the loser.  But it does not work for the winner.
Note the points substracted from the loser's starting ELO shlould be added to the winner's starting ELO.

Here is a link to a web site that does it properly:
Elo Calculator Site
Use 16 as the "K factor" to match my results in the bottome part of the page.

Here is my script.  The 2 players have a starting ELO of 2300 and 2100.  The higher rated player wins in my example which you can run as HTML.

The looser loses 4 points correctly.  But the winner gains 19?  The winner should gain +4 points and end up with 2304.

Here is the code
<html>
<head>

  <title>ELO Calculator</title>

</head>


<body  bgcolor="#FFFFFF">


  <h1>

      <script type="text/javascript">
       var playerElos = [];
	    playerElos[1] = 2300;
	    playerElos[-1] = 2100;

/*  these are the starting ELO of 2 players ("1" and "-1")
     The winner is designated below    */

        var winner = 1


		var expected={
			"1": 1/(1+(Math.pow(10,(playerElos[-1]-playerElos[1])/400))),
			"-1": 1/(1+(Math.pow(10,(playerElos[1]-playerElos[-1])/400))),
			}
			var score;
				   switch(winner) {
				   case 1: score={"1":2,"-1":0}; break;
				   case -1: score={"1":0,"-1":2}; break;
				   default: score={"1":1,"-1":1}; break;
			}
			var newElos={
					"1": playerElos[1]+16*(score[1]-expected[1]),
					"-1": playerElos[-1]+16*(score[-1]-expected[-1]),
			}


	document.write("Hello World! <br/>");
    document.write(newElos[1] + "<br/>");
    document.write(newElos[-1] + "<br/>");
    document.write("Hello World! <br/>");


      </script>

   </h1>


</body>

</html>

Open in new window


Can anyone see why this is happening?

Many thanks in advance,
hefterr
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The case statement is just assigning a score depending on who is the winner.

I don't know enough about ELO's to say what it is doing but based on your test data when winner=1 then
score[1] = 2 and score[-1] = 0
Avatar of hefterr

ASKER

Hi Julian,
Thanks for you help.  I found the problem,

The code in the script
I found the problem:

The original code:


var score;
       switch(winner) {
       case 1: score={"1":2,"-1":0}; break;
       case -1: score={"1":0,"-1":2}; break;
       default: score={"1":1,"-1":1}; break;
}

Open in new window


S/B
     
  case 1: score={"1":1,"-1":0}; break;
        case -1: score={"1":0,"-1":1}; break;
        default: score={"1":.5,"-1":.5}; break;

Open in new window


This works fine now.


Thanks,
again.
Avatar of hefterr

ASKER

Thanks again for your help.

rbh
You are welcome.