Skip to content

Instantly share code, notes, and snippets.

@mhull
Last active February 1, 2017 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhull/a006c3de6e34203bebde3b9a5a23999e to your computer and use it in GitHub Desktop.
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
var coinTosses = [
{ isHeads: true, user: 'Michael', time: 1485954569 },
{ isHeads: false, user: 'Michael', time: 1485954575 },
/* ... etc ... */
];
var condition = function( coinToss ) {
return coinToss.isHeads;
}
var allHeads = true;
for( var i = 0; i < coinTosses.length; i++ ) {
if( ! condition( coinTosses[i] ) ) {
allHeads = false;
}
}
var allHeads = coinTosses.every( condition );
myArray.every( condition ) === ! myArray.some( ! condition )
myArray.some( condition ) === ! myArray.every( ! condition )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment