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


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

Updated on: 17-Apr-2021

335 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements