Finding upper elements in array in JavaScript


We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should return an array of all the elements from the input array that are greater than or equal to the number taken as the second argument.

Therefore, let’s write the code for this function −

Example

The code for this will be −

const arr = [56, 34, 2, 7, 76, 4, 45, 3, 3, 34, 23, 2, 56, 5];
const threshold = 40;
const findGreater = (arr, num) => {
   const res = [];
   for(let i = 0; i < arr.length; i++){
      if(arr[i] < num){
         continue;
      };
      res.push(arr[i]);
   };
   return res;
};
console.log(findGreater(arr, threshold));

Output

The output in the console will be −

[ 56, 76, 45, 56 ]

Updated on: 21-Oct-2020

84 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements