Statistics Calculator Step 51

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;
}

Welcome to the forum @JacobN

The red squiggly line in the editor indicates you have a syntax error.

Happy coding

If I remove the “, 0” the error I get is:
Your reduce callback should have an initial value of 0

Initialization should typically be:
array.reduce((a, b) => a + b, 0);

But if I put:
const variance = array.reduce((acc, el) => 0);
or
const variance = array.reduce((acc, el) => {0});

The error I get is:
Your reduce callback should be an empty function

Are you sure that is the the typical structure for .reduce() is correct?

From the getMean() section of this lesson it shows:
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;

No syntax issues but error says: " Your reduce callback should be an empty function."
const variance = array.reduce((acc, el) => “”, 0);

I don’t understand how the callback can be both empty and also include setting initial value to 0. Or maybe I’m misunderstanding what the function body is, I thought I did

The acc + el part was the function body part.

OK so I understand that correctly. The instructions for this step say: " For the callback, pass in your standard acc and el parameters, but leave the function body empty for now."

const variance = array.reduce((acc, el) => {}, 0);

I got it. Thanks for bearing with me. The error " You should remove the differences variable." when I clearly had I think sent me into a tailspin of confusion.