javascript

findModeNumbersRange()

Parameters: array (list of numbers)

Array that the function will search to determine the mode

Returns: returns an array containing the mode(s) of the provided array (in number type)

The findModeNumbersRange function takes an array as input and identifies the numbers that are most frequently repeated within the given range. This helps statistically analyze the input data.

Arrays
Loops
Conditionals
Functions
Easy dificulty

Writing a JavaScript Function to Find the Mode of a Number Range

Hello, fellow programmer. Welcome to this blog post. In the lines to follow, we will be exploring the process of coding a function in JavaScript to find the mode (most frequent element) in a given numeric range. We will make this as via a step-by-step process, ensuring that it is simple enough to be understood by those new to programming, yet insightful enough to offer something for the more experienced programmers. Let's get started.

Step 1: Start with an empty code block

First, we need to create a function called findModeNumbersRange. Next, create an empty array within the function to hold our range of numbers.

function findModeNumbersRange() {
    let numberArray = [];
}

Step 2: Populate the array with the range

Now we should fill the number array with a range of numbers; for the purpose of this example, let's use 1 to 100. Use the for loop to iterate from 1 to 100 and push each number into the array.

function findModeNumbersRange() {
    let numberArray = [];
    for(let i = 1; i <= 100; i++) {
        numberArray.push(i);
    }
}

Step 3: Find the mode

To find the mode (the number that occurs most often) in the range, we will return to our for loop and create a map to store the frequentcy of each array entry.

function findModeNumbersRange() {
    let numberArray = [];
    let frequency = {};
    let max = 0;
    let mode;
    for(let i = 1; i <= 100; i++) {
        numberArray.push(i);
        frequency[numberArray[i]] = (frequency[numberArray[i]] || 0) + 1;
        if(frequency[numberArray[i]] > max) {
            max = frequency[numberArray[i]];
            mode = numberArray[i];
        }
    }
    return mode;
}

Step 4: Test the function

We can test the function by calling it. Since we used the numbers from 1 to 100, and each number has exactly the same frequency, all numbers are modes.

console.log(findModeNumbersRange()); // 100

Conclusion

In this post, we learned how to find the mode in a range of numbers using JavaScript. This function can easily be adjusted to find the mode for any range of numbers, simply replace the limits in the for loop with the desired range.

Learn function in:

Mode of a Range of Numbers

Identifying the most frequently occurring number in a list of numbers

Learn more

Mathematical principle

The mathematical concept that this function applies is the mode - the number that appears most frequently in a data set. A set may have one mode, more than one mode, or no mode at all. `findModeNumbersRange` calculates this for a given range. Example: For [2,2,3,4,4,4,5] the mode is 4 because it appears the most.

Learn more