Posted in freecodecamp, tutorial, web dev

Learn Advanced Array Methods – Part 3

Building a Statistics Calculator (Step 21 – 27)

Freecodecamp.com Tutorial

**This is not meant to be a tutorial or a guide on how to complete the tutorial. This is just my thought process and notes as I go through the steps myself.**

As a reminder, I am working to find the median of the array next. I’m going to be using the modulus operator to check if the array’s length is even or odd. This is a simple expression that divides the number I want to check (in this instance, the length of the array) by 2. If there is a remainder, it is an odd number. If not, it is even. To clarify, modulus divides the numbers, but it returns the remainder, not the result of the division. So you’re just checking for 0 or 1. Examples:

// check if array length is even
arr.length % 2 === 0;

// check if array length is odd
arr.length % 2 === 1;

In the project I’m building, I’m going to create an isEven variable that uses modulus to check if the length is an even or odd amount, and obviously I will use the expressions shown above. Evaluating the expression will return true or false.

To get the middle number of an array with an odd amount of elements, I will need to do a few things. First we need to get the number of elements of the array by using testArr1.length.

Once the length of the array is known, the next step is to divide it by 2 to get to the middle. A length with an odd amount of elements will return a decimal number, so Math.floor() will need to be used to change that decimal to the nearest integer less than or equal to it.

Finally, all of that needs to be wrapped in testArr1[] so that the index in the middle of the array will be returned, allowing the value of the element to be accessed.

testArr1[Math.floor(testArr1.length / 2)];

A similar method is used when the length of the array is an even amount, however the two numbers in the middle need to be accessed, not just one.

testArr2[(testArr1.length / 2)];
testArr2[(testArr1.length / 2) - 1];

Once the two middle values are gathered, the mean of the two numbers must be found to get the median. For this, the function we already have to get the mean for the first part of the calculator can be called. That way code isn’t rewritten and it keeps everything cleaner.

const evenListMedian = getMean([testArr2[testArr2.length /2], testArr2[(testArr2.length / 2) - 1]])

On this next step it is asking me to apply what I learned on the previous step to the actual function (before it was just practice). I thought I did that but it is still telling me it isn’t correct. Now I get to figure out why..

const getMedian = (array) => {
  const sorted = array.sort((a, b) => a - b);
  if (sorted.length / 2 === 0) {
    return getMean([sorted[sorted.length / 2 - 1], sorted[sorted.length / 2]]);  
  }
}

First error I think was that I used / 2 instead of % 2 in the if condition so I fixed that.

Oh, I forgot to put the code for if the length is odd. Oops.

const getMedian = (array) => {
  const sorted = array.slice().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;
}

There we go, fixed. I also added .slice() before .sort() to create a ‘shallow copy’ of the array so we don’t mutate the original. Then we just need to update the text content of the #median element and that part is all finished.

Author:

Web Developer with art and design experience - recently laid off and using my free time to learn more.