Skip to content

Instantly share code, notes, and snippets.

@caderek
Created December 22, 2022 17:45
Show Gist options
  • Save caderek/4073ce60494839384c3fa1cdc1c8e215 to your computer and use it in GitHub Desktop.
Save caderek/4073ce60494839384c3fa1cdc1c8e215 to your computer and use it in GitHub Desktop.
const NUMBER_OF_FORMULAS = 1000;
const generateName = (num) => {
const charCode = (num % (122 - 96)) + 97;
const count = Math.floor(num / 26);
return String.fromCharCode(charCode).repeat(count + 1);
};
const formulas = new Map(
Array.from({ length: NUMBER_OF_FORMULAS }, (_, i) => [
generateName(i),
(x, y) => x + y + i,
])
);
function calculateWithMap(formula, x, y) {
let calcResult = 0;
if (formula === "average") {
for (const [_, formula] of formulas) {
calcResult += formula(x, y);
}
calcResult /= formulas.size;
} else {
const calc = formulas.get(formula);
if (calc) {
calcResult = calc(x, y);
}
}
return Math.round(calcResult);
}
export default calculateWithMap;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment