# JavaScript Challenge - Calculate the Mean and Mode, Calculate the Mean and Mode

``````function statsFinder(array) {
const mn = array.reduce((i, n) => i + n) / array.length;
const o = {};
array.forEach(n => o[n] ? o[n]++ : o[n] = 1);
const ms = parseInt(Object.keys(o).reduce((a, b) => o[a] > o[b] ? a : b));
return [mn, ms];
}

console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300]))

// Leave this so we can test your code:
module.exports = statsFinder;
``````
``````function findMapMax(map) {
let maxValue = 0;
let maxKey = 0;
map.forEach((value, key) => {
if (value > maxValue) {
maxValue = value;
maxKey = key;
}
})
return maxKey;
}

function statsFinder(array) {
const meanAndMode = array.reduce((a,b) => {
a["mean"] += b;
a["mode"].set(b, a["mode"].has(b) ? a["mode"].get(b) + 1 : 1);
return a;
}, {"mean" : 0, "mode" : new Map()});

return [
meanAndMode["mean"] / array.length,
findMapMax(meanAndMode["mode"])
]
}

console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300]))

// Leave this so we can test your code:
module.exports = statsFinder;
``````
``````function statsFinder(array) {
let meanAndMode = []
const mean = array.reduce((a,b) => (a + b)) / array.length;
const mode = array.find((i, index) => {
return array.lastIndexOf(i) != index;
})

meanAndMode.push(mean, mode)

return meanAndMode;
}

console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300]))

// Leave this so we can test your code:
module.exports = statsFinder;
``````