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

Tell us what’s happening:

SOS, I am not sure what else its asking of me. Can I get another set of eyes on this

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: 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;
}
/* file: script.js */
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;


// User Editable Region

const getMedian = (array) => {
  const sorted = array.sort((a, b) => a - b);
  if (sorted(array.length % 2 - 1) === 0) {
    const oddListMedian = array[Math.floor(array.length / 2)];
    const firstMiddleNumber = array[array.length / 2];
    const secondMiddleNumber = array[(array.length / 2) - 1];
    const evenListMedian = getMedian([firstMiddleNumber, secondMiddleNumber]);
    return evenListMedian;
  } if (array.length % 2 === 0) {
    const middleNumber = array[Math.floor(array.length % 2 === 0)];
    return middleNumber;
  };
}

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

Your browser information:

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

Challenge Information:

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

HI @LowVel0city !

You have a few issues here

First issue is here

it looks like you are trying to see if the list if even.
But is not quite right
Refer back to this previous step here on the correct way to check if the list is even.
It shows you in the directions

Once you fix that, then you need to fix this

You are trying to call getMedian inside getMedian
My guess is that you were trying to use the other function defined at the top of the page.
You will need to update that

Third issue is here

that if statement is currently checking if the list is even.
But based on your code earlier, it looks like you were trying to get the median for an even list there too.
So maybe you wanted to check if the list was odd?
If so, then I would personally use an else because either the list is even or not.
Or if you want to use another if or else if here, then you will want to use the correct way to check for odd numbered lists

Once that is solved, you will need to update this part here

It looks like you are trying to check the middle number.
But you already calculated the middle number earlier here

So I would suggest move that oddListMedian out of that first if statement, since you are not using it and inside the second if statement that you need it for.

once I make all of those changes, it passes for me

hope that helps

1 Like

I now have the below, assuming I’ve made the correct changes. It does not pass, but I am not confident the right changes were made. Not that your explanation was confusing, I just don’t understand why some of those changes needed to be done/moved or how that effects the code.

const getMedian = (array) => {
  const sorted = array.sort((a, b) => a - b);
  if (sorted(array.length % 2) === 0) {
    const firstMiddleNumber = array[array.length / 2];
    const secondMiddleNumber = array[(array.length / 2) - 1];
    const evenListMedian = getMean([firstMiddleNumber, secondMiddleNumber]);
    return evenListMedian;
  } else (array.length % 2 - 1 === 0) {
    const middleNumber = array[Math.floor(array.length % 2 === 0)];
    const oddListMedian = array[Math.floor(array.length / 2)];
    return middleNumber;
  };
}

It looks like you are getting closer but you have some logic and syntax errors still.

Whenever you are stuck, it is best to console statement to better understand what your code is doing. That will give you a better idea what is wrong.

For example, sorted is an array. not a function.
but you are treating this like a function

I would suggest reviewing the lesson again on how to check if an array length is even because you are overcomplicating it here.
Pay close attention to the syntax used in that lesson, and you will see that you are adding extra things that don’t need to be there.

You can also google how to check if number is even javascript and you will get articles like this

I think looking at some other example will help you understand where you are going wrong.

Once you fix that, then you have this issue

that is not the correct syntax for else statements

last issue is here

you middle number calculation is not correct.
but also, you are not using the oddListMedian calculation at all.

I think what will help you more are two things

  1. Writing down on pen and paper exactly what the steps are to solving the problem at hand. I think right now, you are not entirely sure how to solve this problem without code. You should have a clear algorithm in your head first on how to get the median for a list of numbers. You should write down those steps in plain english. Then once you have a clear process, then you slowly turn that into code. That will give you a better understanding on how to approach this problem and you will need to use that process when you work on harder problems.

  2. When you are not sure how to do something, make sure you are looking at multiple sources. For example, how to check if an array is even or not. Or more simply put, how to check if a number is even in javascript. Reading through multiple articles, videos and documentation will show you a few examples on how to do things, and will better solidify how to solve these problems.

Hope that helps