Integers Greater Than

Easy

Prompt

Create a function named integersGreaterThan that takes two arguments:

  • n: A number
  • arr: An array of integers

The function should return a new array of integers from arr that are greater than n.

Example

integersGreaterThan(10, [10, 5, 67, 23, 88, 15]); // [67, 23, 88, 15]

Playground

Hint

The filter method is a built-in array method in JavaScript that creates a new array with all elements that pass the test implemented by the provided function. This is a perfect fit for the problem at hand, where we need to create a new array containing only the integers greater than n.

Solution

Explanation

  • The function uses the filter method on the arr array.
  • The filter method creates a new array with only the elements from arr that pass a certain condition.
  • The condition is defined by an arrow function: (num) => num > n.
  • This arrow function checks if each number num in the arr array is greater than n.
  • If num is greater than n, it is included in the new filtered array.
  • If num is not greater than n, it is excluded from the new filtered array.
  • The new filtered array is returned by the integersGreaterThan function.

So, in simple terms, the integersGreaterThan function takes a number and an array, and returns a new array containing only the elements from the original array that are greater than the given number.

00:00