Learn Advanced Array Methods by Building a Statistics Calculator - Step 35

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

Hi there!

You need first to assign the value before the ternary condition, as instructions example.

1 Like

Hi. Look at the example code in the step. You need the first part of the code and the = sign. Have a look at step 34 for a reminder of what you are doing. You are assigning a value to counts[el]. Like any assignment operator, you only put the = sign once. Re do your expressions if true / false again so it has the correct value without repeating the = sign.

1 Like