@matsos/expected-wins
Version:
A simple javascript library based on a formula developed by baseball statistician Bill James. It attempts to determine the number of games a team should have won, by comparing the total number of runs a team has scored to the number of runs it has allowed
12 lines (9 loc) • 471 B
JavaScript
module.exports = function basicExpectedWins(runsScored, runsAllowed) {
// make sure both inputs are numbers
if (typeof runsScored !== "number" || typeof runsAllowed !== "number")
throw new TypeError("Both runsScored and runsAllowed should be numbers");
// calculate the expected win percentage
result = 1 / (1 + Math.pow(runsAllowed / runsScored, 2));
// round the pecentage to 3 decimal places and return it
return Math.round(result * 1000) / 1000;
};