
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 ]
Advertisements