Solved

Help with a small Javascript

Posted on 2016-10-15
6
72 Views
Last Modified: 2016-10-16
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
0
Comment
Question by:hefterr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 58

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41845113
Your algorithm is not the same as theirs
Here is there's
function CalculateRatingChange()

{

// Ensure Elo1 is an number
var Elo1 = document.rating.elo1.value * 1;

// Ensure Elo2 is an number
var Elo2 = document.rating.elo2.value * 1;

// Ensure k value is a number
var K = document.rating.K.value * 1;

// Take the difference in the Elo's
var EloDifference = Elo2 - Elo1;

// This is where they do one calc and you do 2
// basically in your example works out to
// 1 / ( 1 + 10^.5) ~ 75%
var percentage = 1 / ( 1 + Math.pow( 10, EloDifference / 400 ) );

// Basically 25% goes to the win
var win = Math.round( K * ( 1 - percentage ) );

var draw = Math.round( K * ( .5 - percentage ) );

// Create the labels
if (win > 0 ) win = "+" + win;

// Create the labels
if (draw > 0 ) draw = "+" + draw;

// Write to the document
document.ratingchange.win.value = win;

document.ratingchange.draw.value = draw;

// 75% goes to the loss - in this example
document.ratingchange.loss.value = Math.round( K * ( 0 - percentage ) );

document.ratingchange.percent.value =  Math.round( percentage * 100 ) + "%";

}

Open in new window

0
 
LVL 1

Assisted Solution

by:hefterr
hefterr earned 0 total points
ID: 41845127
Hi Julian,
Thanks for showing me theirs.

Can you see the error in mine for the winner?  I'm not sure how to interpret the "case" statements in my example.

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


Thanks again,
hefterr
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41845173
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
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 1

Author Comment

by:hefterr
ID: 41845738
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.
0
 
LVL 1

Author Closing Comment

by:hefterr
ID: 41845741
Thanks again for your help.

rbh
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41846175
You are welcome.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question