Filtering array within a limit JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

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 ]
raja
Published on 23-Nov-2020 06:21:41
Advertisements