Integers Greater Than
Easy
Prompt
Create a function named integersGreaterThan
that takes two arguments:
n
: A numberarr
: 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 thearr
array. - The
filter
method creates a new array with only the elements fromarr
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 thearr
array is greater thann
. - If
num
is greater thann
, it is included in the new filtered array. - If
num
is not greater thann
, 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