Skip to content

Instantly share code, notes, and snippets.

@npgenx
Last active April 30, 2022 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save npgenx/9b49487b40b8268f6d4d78da366d7d03 to your computer and use it in GitHub Desktop.
Save npgenx/9b49487b40b8268f6d4d78da366d7d03 to your computer and use it in GitHub Desktop.
js: Get mean & mode
const statsFinder = (array) => {
const total = array.reduce((nextitem, previtem) => (nextitem + previtem),0);
const mean = total / array.length;
const getMode = (array) => {
const modeObject = {};
array.forEach(number => {
modeObject[number] = (!modeObject[number]) ? 1 : modeObject[number]+1;
});
return +Object.keys(modeObject).reduce((a, b) => modeObject[a] > modeObject[b] ? a : b);
}
const mode = getMode(array);
return [mean, mode];
}
statsFinder([500, 400, 400, 375, 300, 350, 325, 300])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment