Working on a programming challenge to re-implement the functionality of underscore.js in standard javascript. Specifically I am working on implementing the _.some
function. (http://underscorejs.org/#some)
The part I'm struggling with is it's asking me to find a way to solve it using _.every
internally. (http://underscorejs.org/#every)
I have already finished the _.every
function earlier and it works as it should.
Here is logically what I am wanting to do in sketched code:
_.some = function(collection, truthStatementFunction) {
return !(_every(collection, !truthStatementFunction))
}
Or in english, flip the truth statement to test where the condition is false... and if the _.every
test then returns true... then we know that _.some of the original truth statement is false (so flip the return of _.every
to get the correct return for _some
). Likewise if _.every
returns false then flip that to get the correct return of true for _.some
.
Obviously the problem with this sketch is the !truthStatementFunction
part. How do I get inside that iterator to change the internals of the function to flip it? Doesn't seem that the internals of the function are accessible...
Am I barking up the wrong tree entirely, and there is a better way to solve this using _.every
?