Hello! I’m running into an error where something seems stuck. The error I’m getting states " You should remove the differences
variable."
Step 51 states: Remove the differences
, squaredDifferences
, and sumSquaredDifferences
variables (and their values). Declare a variance
variable, and assign it the value of array.reduce()
. For the callback, pass in your standard acc
and el
parameters, but leave the function body empty for now. Don’t forget to set the initial value to 0
.
Here is my script.js contents:
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.toSorted((a, b) => a - b);
const median =
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
const getMode = (array) => {
const counts = {};
array.forEach((el) => {
counts[el] = (counts[el] || 0) + 1;
})
if (new Set(Object.values(counts)).size === 1) {
return null;
}
const highest = Object.keys(counts).sort(
(a, b) => counts[b] - counts[a]
)[0];
const mode = Object.keys(counts).filter(
(el) => counts[el] === counts[highest]
);
return mode.join(", ");
}
const getRange = (array) => {
return Math.max(...array) - Math.min(...array);
}
const getVariance = (array) => {
const mean = getMean(array);
const variance = array.reduce((acc, el, 0) => {});
}
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);
const mode = getMode(numbers);
const range = getRange(numbers);
document.querySelector("#mean").textContent = mean;
document.querySelector("#median").textContent = median;
document.querySelector("#mode").textContent = mode;
document.querySelector("#range").textContent = range;
}