every method to check an array in JavaScript

The following method every checks the array elements and returns true if the callback returned true for all array elements, otherwise the method returns false. Let's check, for example, that all elements of the array are even numbers:

let arr = [2, 4, 6, 8]; let result = arr.every(function(elem) { if (elem % 2 == 0) { return true; } else { return false; } }); console.log(result);

Let's simplify:

let arr = [2, 4, 6, 8]; let result = arr.every(function(elem) { return elem % 2 == 0; }); console.log(result);

Let's simplify even more:

let arr = [2, 4, 6, 8]; let result = arr.every(elem => elem % 2 == 0); console.log(result);

Given an array of numbers. Check that all elements in the array are greater than zero.

Given an array of numbers. Check that for all elements the product of their values and their index numbers is less than 30.

enru