Skip to content

Instantly share code, notes, and snippets.

@caderek
Created December 22, 2022 17:06
Show Gist options
  • Select an option

  • Save caderek/d69b8d4b72bb54cf59ad64ec1b52a6f0 to your computer and use it in GitHub Desktop.

Select an option

Save caderek/d69b8d4b72bb54cf59ad64ec1b52a6f0 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") {
formulas.forEach((formula) => {
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