diff --git a/README.md b/README.md index 91e2025e8..162d88509 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,7 @@ * [`collatz`](#collatz) * [`digitize`](#digitize) * [`distance`](#distance) +* [`elo`](#elo) * [`factorial`](#factorial) * [`fibonacci`](#fibonacci) * [`fibonacciCountUntilNum`](#fibonaccicountuntilnum) @@ -2448,6 +2449,39 @@ distance(1, 1, 2, 3); // 2.23606797749979
[⬆ Back to top](#table-of-contents) +### elo + +Computes the new ratings between two opponents using the [Elo rating system](https://en.wikipedia.org/wiki/Elo_rating_system). It takes an array +of two pre-ratings and returns an array containing two post-ratings. +The winner's rating is the first element of the array. + +Use the exponent `**` operator and math operators to compute the expected score (chance of winning) +of each opponent and compute the new rating for each. Omit the second argument to use the default +K-factor of 32, or supply a custom K-factor value. + +```js +const elo = ([a, b], kFactor = 32) => { + const expectedScore = (self, opponent) => 1 / (1 + 10 ** ((opponent - self) / 400)); + const newRating = (rating, i) => rating + kFactor * (i - expectedScore(i ? a : b, i ? b : a)); + return [newRating(a, 1), newRating(b, 0)]; +}; +``` + +
+Examples + +```js +elo([1200, 1200]); // [1216, 1184] +elo([1000, 2000]); // [1031.8991261061358, 1968.1008738938642] +elo([1500, 1000]); // [1501.7036868864648, 998.2963131135352] +elo([1200, 1200], 64); // [1232, 1168] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### factorial Calculates the factorial of a number. diff --git a/docs/index.html b/docs/index.html index 1e956217b..ed310daf9 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+    }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -483,6 +483,15 @@ collatz(5); // 16
 
digitize(123); // [1, 2, 3]
 

distance

Returns the distance between two points.

Use Math.hypot() to calculate the Euclidean distance between two points.

const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0);
 
distance(1, 1, 2, 3); // 2.23606797749979
+

elo

Computes the new ratings between two opponents using the Elo rating system. It takes an array of two pre-ratings and returns an array containing two post-ratings. The winner's rating is the first element of the array.

Use the exponent ** operator and math operators to compute the expected score (chance of winning) of each opponent and compute the new rating for each. Omit the second argument to use the default K-factor of 32, or supply a custom K-factor value.

const elo = ([a, b], kFactor = 32) => {
+  const expectedScore = (self, opponent) => 1 / (1 + 10 ** ((opponent - self) / 400));
+  const newRating = (rating, i) => rating + kFactor * (i - expectedScore(i ? a : b, i ? b : a));
+  return [newRating(a, 1), newRating(b, 0)];
+};
+
elo([1200, 1200]); // [1216, 1184]
+elo([1000, 2000]); // [1031.8991261061358, 1968.1008738938642]
+elo([1500, 1000]); // [1501.7036868864648, 998.2963131135352]
+elo([1200, 1200], 64); // [1232, 1168]
 

factorial

Calculates the factorial of a number.

Use recursion. If n is less than or equal to 1, return 1. Otherwise, return the product of n and the factorial of n - 1. Throws an exception if n is a negative number.

const factorial = n =>
   n < 0
     ? (() => {