Last active
March 27, 2021 01:11
-
-
Save omarbenmegdoul/d41db78ff6c81cd69c81ab461005f08f to your computer and use it in GitHub Desktop.
someArray.every(callback) explainer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This program will take an array of numbers and return true if they're all divisible by 3, else false. | |
//.every is used for obtaining a boolean -- not an array -- if every element passes a condition | |
//So there is a final variable that stores the boolean output (therefore we need a return) | |
//another way to explain it: every() returns true when filter() would return the same array | |
//we can use the "arrow function" shorthand to rewrite the function definition in a shorter way | |
let baseArray = [1,2,3,4,5,6,7,8,9,0]; | |
let finalBoolean = true; //just declaring this variable for later | |
finalBoolean = baseArray.every( | |
num => (num % 3 === 0) //this function always returns true or false | |
) | |
//console.log(finalBoolean) //to see the output, you'd have to un-comment this line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment