
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
Filtering Array Within a Limit in JavaScript
We are required to write a JavaScript function that takes in an array of numbers as the first argument and an upper limit and lower limit number as second and third argument respectively. Our function should filter the array and return a new array that contains elements between the range specified by the upper and lower limit (including the limits)
Example
const array = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20]; const lower = 18; const upper = 20; const filterByLimits = (arr = [], upper, lower) => { let res = []; res = arr.filter(el => { return el >= lower && el <= upper; }); return res; }; console.log(filterByLimits(array, upper, lower));
Output
And the output in the console will be −
[ 18, 20, 18, 19, 18, 20 ]
Advertisements