Checking if all array values are smaller than a limit using JavaScript

JavascriptWeb DevelopmentFront End Technology

Problem

We are required to write a JavaScript function that takes in an array of numbers and a number. We should return true if all the numbers in the array are smaller than the number given as second argument, false otherwise.

Example

Following is the code −

 Live Demo

const arr = [5, 34, 23, 14, 78, 45, 78];
const limit = 99;
const checkEvery = (arr = [], limit = 1) => {
   const allSmaller = arr.every(el => {
      return el < limit;
   });
   return allSmaller;
};
console.log(checkEvery(arr, limit));

Output

true
raja
Published on 17-Apr-2021 13:14:58
Advertisements