switch to exponent operator

This commit is contained in:
atomiks
2017-12-31 19:04:35 +11:00
parent cb0d65826e
commit dc1322f72b

View File

@@ -4,13 +4,13 @@ Computes the new ratings between two opponents using the [Elo rating system](htt
of two pre-ratings and returns an array containing two post-ratings. of two pre-ratings and returns an array containing two post-ratings.
The winner's rating is the first element of the array. The winner's rating is the first element of the array.
Use `Math.pow()` and math operators to compute the expected score (chance of winning) of each opponent Use the exponent `**` operator and math operators to compute the expected score (chance of winning)
and compute the new rating for each. Omit the second argument to use the default K-factor of of each opponent and compute the new rating for each. Omit the second argument to use the default
32, or supply a custom K-factor value. K-factor of 32, or supply a custom K-factor value.
```js ```js
const elo = ([a, b], kFactor = 32) => { const elo = ([a, b], kFactor = 32) => {
const expectedScore = (self, opponent) => 1 / (1 + Math.pow(10, (opponent - self) / 400)); const expectedScore = (self, opponent) => 1 / (1 + 10 ** ((opponent - self) / 400));
const newRating = (rating, i) => rating + kFactor * (i - expectedScore(i ? a : b, i ? b : a)); const newRating = (rating, i) => rating + kFactor * (i - expectedScore(i ? a : b, i ? b : a));
return [newRating(a, 1), newRating(b, 0)]; return [newRating(a, 1), newRating(b, 0)];
}; };