Array Methods

Methods for Conditions

🙋 Need help? Ask an expert now!

The array methods for conditions are complex array methods that require the knowledge of how a function works and calling a function. If you are not familiar with that yet, we will be exploring function in-depth later in the course. The syntax and the function of the every() method and the some() method is shown below.

arrayname.every(function)

  • Returns: boolean
    • Returns true if ALL the elements of the array meets the criteria in the function
    • Returns false if ANY of the elements does not meet the criteria in the function

arrayname.some(function)

  • Returns: boolean
    • Returns true if ANY of the elements of the array meets the criteria in the function
    • Returns false if NONE of the elements meets the criteria in the function

The every() and some() method above checks each element against isEvenNumber() and returns true or false depending on the number of elements that met the criteria. This is why the first every() method returns false while the second one returns true. This is because odd numbers exits in arr 1, meaning more than one element will return false after executing the function isEvenNumber(). The some() method will return true if some of the elements meet the criteria while the every() method requires every element to meet the criteria.

Edit Me on GitHub!

Application Question

Consider the following code segment:

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
            function isEvenNumber(number) {
                return number % 2 == 0;
            }
            function isOddNumber(number) {
                return number % 2 == 1;
            }
            var booleanOne = arr.every(isEvenNumber) && arr.some(isOddNumber);
            var booleanTwo = arr.some(isEvenNumber) && arr.every(isOddNumber);
            console.log(booleanOne);
            console.log(booleanTwo);
            

What will the following code segment print to the console?