Tell us what’s happening:
Merry Christmas FCC Gang!
there is no rest for the wicked, hence why I am grinding FCC JS on Christmas Day!
Could one of you help me with my code?
Your code so far
/* file: script.js */
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
return median;
}
// User Editable Region
const getMode = (array) => {
const counts = {};
array.forEach((el) => counts[el] ? counts[el] += 1 : counts[el] = 1);
return counts;
};
// User Editable Region
const calculate = () => {
const value = document.querySelector("#numbers").value;
const array = value.split(/,\s*/g);
const numbers = array.map(el => Number(el)).filter(el => !isNaN(el));
const mean = getMean(numbers);
const median = getMedian(numbers);
console.log(getMode(numbers));
document.querySelector("#mean").textContent = mean;
document.querySelector("#median").textContent = median;
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Challenge Information:
Learn Advanced Array Methods by Building a Statistics Calculator - Step 35