Last active
April 30, 2022 00:38
-
-
Save npgenx/9b49487b40b8268f6d4d78da366d7d03 to your computer and use it in GitHub Desktop.
js: Get mean & mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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