Start learning free

LESSON 116 OF 671 · ARRAYS - CHALLENGES

Middle Element in JavaScript: Math.floor and Indexes

Practice Read the full explanation

Given an array of integers nums with an odd number of items, return the item in the middle.

The middle index is the length of nums divided by 2, rounded down. For example, 5 items have the indexes 0 to 4, and 5 divided by 2, rounded down, is 2: two items come before index 2 and two come after it.

 

Example 1:

Input: nums = [1, 2, 3]
Output: 2
Explanation: The length is 3, and 3 divided by 2, rounded down, is 1. The item at index 1 is 2.

Example 2:

Input: nums = [5, 9, 1, 8, 4]
Output: 1
Explanation: The middle index is 2, and the item there is 1. The middle is about the position, not the size of the number.

Example 3:

Input: nums = [7]
Output: 7
Explanation: 1 divided by 2, rounded down, is 0: the only item is the middle one.

 

Constraints:

  • nums has an odd number of items: 1, 3, 5 and so on.
  • Every number is an integer between -1000 and 1000.
STEP 1 OF 2
    Ctrl + Enter to check

    JAVASCRIPT · ARRAYS - CHALLENGES

    Middle Element in JavaScript, explained

    Problem overview

    The method middleElement receives an array of integers, nums, with an odd number of items, and returns the item in the middle.

    For [1, 2, 3] the answer is 2. For [5, 9, 1, 8, 4] it is 1: the middle is about the position, not the size of the number. For [7] the only item is the middle one, so the answer is 7.

    Approach

    With an odd length, the middle index is the length divided by 2, rounded down. Five items have the indexes 0 to 4. Then 5 / 2 is 2.5, and rounding down gives 2: two items come before index 2 and two come after it.

    In JavaScript, / always gives a floating-point result, so 5 / 2 is 2.5, not 2. Math.floor rounds down to a whole number. The rounding matters because an array index must be an integer: [1, 2, 3][1.5] is undefined.

    Step by step

    1. Find the middle index. Divide nums.length by 2, round down with Math.floor and store it in middle, declared with let.
    2. Return the item at index middle.

    Rounding in practice

    For 7 items, 7 / 2 is 3.5 and Math.floor(3.5) is 3. Indexes 0, 1 and 2 come before it and indexes 4, 5 and 6 come after it, so index 3 is the middle. For one item, 1 / 2 is 0.5 and Math.floor(0.5) is 0.

    The solution in JavaScript

    class Solution {
        middleElement(nums) {
            let middle = Math.floor(nums.length / 2);
            return nums[middle];
        }
    }
    

    On the examples it returns:

    [1, 2, 3]       -> 2
    [5, 9, 1, 8, 4] -> 1
    [7]             -> 7
    

    Choosing the rounding

    Math.floor and Math.trunc agree for positive numbers: both give 3 for 3.5. They differ for negative ones: Math.floor(-0.5) is -1 and Math.trunc(-0.5) is 0. A length is never negative, so either works here, and the lesson uses Math.floor, which matches the statement's "rounded down".

    Thinking about the problem

    The statement gives the rule: the middle index is the length divided by 2, rounded down. The array has an odd number of items, so there is exactly one item with the same number of items on each side. Compute the index first and store it in a named variable, then use it. Naming the index separates two ideas, where the middle is and what is stored there, and makes each easy to check on its own.

    Printing middle for a few lengths is a quick check: 1 item gives 0, 3 items give 1, 5 items give 2, and 7 items give 3. In each case it is the index with equally many items on both sides.

    Edge cases and common mistakes

    • Skipping Math.floor. nums[nums.length / 2] with 5 items reads index 2.5, which gives undefined.
    • Returning the index. middle is a position. The function returns the item stored there.
    • A single item. The length is 1, Math.floor(0.5) is 0, and index 0 holds the only item.
    • Even lengths. They are outside the lesson: there is no single middle item, and this formula would pick the second of the two central ones.

    Testing

    • [-3, 8, 0] gives 8.
    • [10, 20, 30, 40, 50] gives 30.
    • [1, 2, 3, 4, 5, 6, 7] gives 4.
    Keep going with JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free