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

Tell us what’s happening:

Hello,
I’ve tried with if statements and ternary operators but I cannot manage to pass this step and I don’t understand what’s going wrong.
Please help.

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="./styles.css" />
    <script src="./script.js"></script>
    <title>Statistics Calculator</title>
  </head>
  <body>
    <h1>Statistics Calculator</h1>
    <p>Enter a list of comma-separated numbers.</p>
    <form onsubmit="calculate(); return false;">
      <label for="numbers">Numbers:</label>
      <input type="text" name="numbers" id="numbers" />
      <button type="submit">Calculate</button>
    </form>
    <div class="results">
      <p>
        The <dfn>mean</dfn> of a list of numbers is the average, calculated by
        taking the sum of all numbers and dividing that by the count of numbers.
      </p>
      <p class="bold">Mean: <span id="mean"></span></p>
      <p>
        The <dfn>median</dfn> of a list of numbers is the number that appears in
        the middle of the list, when sorted from least to greatest.
      </p>
      <p class="bold">Median: <span id="median"></span></p>
      <p>
        The <dfn>mode</dfn> of a list of numbers is the number that appears most
        often in the list.
      </p>
      <p class="bold">Mode: <span id="mode"></span></p>
      <p>
        The <dfn>range</dfn> of a list of numbers is the difference between the
        largest and smallest numbers in the list.
      </p>
      <p class="bold">Range: <span id="range"></span></p>
      <p>
        The <dfn>variance</dfn> of a list of numbers measures how far the values
        are from the mean, on average.
      </p>
      <p class="bold">Variance: <span id="variance"></span></p>
      <p>
        The <dfn>standard deviation</dfn> of a list of numbers is the square
        root of the variance.
      </p>
      <p class="bold">
        Standard Deviation: <span id="standardDeviation"></span>
      </p>
    </div>
  </body>
</html>
/* file: script.js */
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;


// User Editable Region

const getMedian = (array) => {
  const sorted = array.toSorted((a, b) => a - b);
  const median = "";
  if (sorted.length % 2 === 0) {
    median = getMean([sorted[sorted.length/2], sorted[(sorted.length/2) - 1]]);
  } else if (sorted.length % 2 === 1) {
    median = sorted[Math.floor(sorted.length/2)];
  }
  return median;
}

// 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);

  document.querySelector("#mean").textContent = mean;
}
/* file: styles.css */
body {
  margin: 0;
  background-color: rgb(27, 27, 50);
  text-align: center;
  color: #fff;
}

button {
  cursor: pointer;
  background-color: rgb(59, 59, 79);
  border: 3px solid white;
  color: white;
}

input {
  background-color: rgb(10, 10, 35);
  color: white;
  border: 1px solid rgb(59, 59, 79);
}

.bold {
  font-weight: bold;
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36

Challenge Information:

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

Welcome to the forum :wave:

When you run the tests, do you get any error messages?

Read the error messages carefully they will tell you exactly what the problem is.

You should never ignore error messages :+1:

Hello and thanks for your reply. I’m going crazy about this.

So, the error states " Your getMedian function should return the mean of the middle two numbers if the array length is even."

And I think my code is doing just that, isn’t it?

if (sorted.length % 2 === 0) {
    median = getMean([sorted[sorted.length/2], sorted[(sorted.length/2) - 1]]);
  }

I feel a bit dumb since yesterday. Open to any suggestion.
Thanks in advance

Look at the console messages. Scroll down to the end. What do you see there?

I haven’t noticed that before

[TypeError: array.toSorted is not a function]

What does it mean?

Did you change your code since you posted it? If so, please post your updated code. That error message did not show up in the console when I entered your code.

Yes, I did many changes to try understand what’s happening.
At this moment, I reverted back the code to the original one posted above.

the

[TypeError: array.toSorted is not a function]

is still there.

I get something different:

// running tests
2. Your getMedian function should return the mean of the middle two numbers if the array length is even.
3. Your getMedian function should return the middle number if the array length is odd.
// tests completed
// console output
[TypeError: "median" is read-only]
[TypeError: "median" is read-only]

If you had reverted back to the posted code, you wouldn’t be seeing the array.toSorted error. You would be seeing the TypeError posted by @pkdvalis.

This is the code I have now.

I don’t know if it is allowed to attach screenshots, if it’s not please remove it.

Okay. Let’s move past the array.toSorted TypeError. I don’t know why you’re seeing that. When I enter your code, I see the error message you should be paying attention to because that’s where your code is broken: [TypeError: "median" is read-only] Please look carefully at your code. Can you see why that error message is displaying?

Hm, it’s a bit weird I can’t reproduce that. Obviously it is a function.

Did you already try resetting this step?

Please try one of the following steps to move forward.

Click on the “Reset” step button and force a refresh of your page with CTRL + F5 if you’re on Windows then try to paste the code in again. On other systems, please follow the instructions here.

or - Try the step in incognito or private mode.

or - Disable any/all extensions that interface with the freeCodeCamp website (such as Dark Mode, Ad Blockers, or Spellcheckers), and set your browser zoom level to 100%. Both of these factors can cause tests to fail erroneously.

or - Ensure your browser is up-to-date or try a different browser.

or - Turn off high contrast themes on Windows (from accessibility settings menu)

I hope one of these will work for you.

Yep, one of the solutions worked.
I’ve tried disabling dark mode, tried the incognito mode in Chrome, then Chrome Dev and then, where everything else failed, I tried Firefox and I did pass with the originary posted code.

Thank you both guys for your precious help.

1 Like

If you passed with the original posted code, something is wrong with the tests, because median was declared as a constant, meaning you cannot change the initial value.

No, you’re right. I used “let” to declare that variable, based on your suggestion.
Thank you.

1 Like