Last active
February 1, 2017 13:45
-
-
Save mhull/a006c3de6e34203bebde3b9a5a23999e to your computer and use it in GitHub Desktop.
Comparing the .some() and .every() functions in JavaScript to the "At Least One" rule
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
var coinTosses = [ | |
{ isHeads: true, user: 'Michael', time: 1485954569 }, | |
{ isHeads: false, user: 'Michael', time: 1485954575 }, | |
/* ... etc ... */ | |
]; |
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
var condition = function( coinToss ) { | |
return coinToss.isHeads; | |
} |
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
var allHeads = true; | |
for( var i = 0; i < coinTosses.length; i++ ) { | |
if( ! condition( coinTosses[i] ) ) { | |
allHeads = false; | |
} | |
} |
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
var allHeads = coinTosses.every( condition ); |
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
myArray.every( condition ) === ! myArray.some( ! condition ) |
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
myArray.some( condition ) === ! myArray.every( ! condition ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment