209k views
5 votes
Define a JavaScript function named numLarger which has two parameters. The first parameter will be an array of Numbers. The second parameter will be a Number. The function must return the number of entries in the first parameter that are larger than the second parameter.

1 Answer

5 votes

Final answer:

The numLarger function takes an array of numbers and counts the entries that are larger than a given number.

Step-by-step explanation:

The subject of this question is Computers and Technology as it requires knowledge of JavaScript programming.

The function numLarger takes two parameters: an array of Numbers and a Number. It counts the number of entries in the array that are larger than the second parameter and returns that count.

For example, if the array is [3, 5, 2, 8, 4] and the second parameter is 4, the function should return 2 because there are two numbers (5 and 8) that are larger than 4.

function numLarger(numbersArray, targetNumber) {

// Use the filter method to create a new array with only the numbers larger than the targetNumber

const largerNumbers = numbersArray.filter((num) => num > targetNumber);

// Return the length of the new array, which represents the count of numbers larger than the targetNumber

return largerNumbers.length;

}

// Example usage:

const numbers = [1, 5, 10, 15, 20];

const target = 10;

const result = numLarger(numbers, target);

console.log(`Number of entries larger than ${target}: ${result}`);

User Mark Chackerian
by
8.4k points