Getting equal or greater than number from the list of numbers 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.

Example

Following is the code −

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

This will produce the following output in console −

[ 56, 76, 45, 56 ]

Updated on: 30-Sep-2020

620 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements