Skip to content

Instantly share code, notes, and snippets.

@mattmazzola
Created November 9, 2020 08:06
Show Gist options
  • Save mattmazzola/58c81b1211c3b1bb0aa0fe79ad5d9573 to your computer and use it in GitHub Desktop.
Save mattmazzola/58c81b1211c3b1bb0aa0fe79ad5d9573 to your computer and use it in GitHub Desktop.
Next Rating
type NextRatingFn = (rating: number, actualPoints: number, expectedPoints: number) => [nextRating: number, ratingChange: number]
type KFactorFunction = (rating: number) => number
function createNextRatingFn(kFactor: number | KFactorFunction): NextRatingFn {
const kFactorFn: KFactorFunction = typeof kFactor === 'number'
? () => kFactor
: kFactor
return (rating: number, actualPoints: number, expectedPoints: number): [number, number] => {
const change = Math.round(kFactorFn(rating) * (actualPoints - expectedPoints))
const nextRating = rating + change
return [nextRating, change]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment