javascript - every() some() and functions returning functions -
i working through nodeschool's functional javascript course. "every some" exercise provides "goodusers" array of objects argument compare list of users (also array of objects).
i hoping if me visualize going on in solution:
function checkusersvalid(goodusers) { return function allusersvalid(submittedusers) { return submittedusers.every(function(submitteduser) { return goodusers.some(function(gooduser) { return gooduser.id === submitteduser.id; }); }); }; } module.exports = checkusersvalid;
these instructions provided:
# task return function takes list of valid users, , returns function returns true if of supplied users exist in original list of users. need check ids match. ## example var goodusers = [ { id: 1 }, { id: 2 }, { id: 3 } ] // `checkusersvalid` function you'll define var testallvalid = checkusersvalid(goodusers) testallvalid([ { id: 2 }, { id: 1 } ]) // => true testallvalid([ { id: 2 }, { id: 4 }, { id: 1 } ]) // => false ## arguments * goodusers: list of valid users use array#some , array#every check every user passed returned function exists in array passed exported function.
i've added annotations briefly describe each functions purpose. wouldn't solve problem way none less explains each part doing
function checkusersvalid(goodusers) { // allusersvalid function compares lists of users against each other // if not match return false if match return true return function allusersvalid(submittedusers) { // .every checks see if every element in array passes test // defined following function if every element passes // return true, , if not return false return submittedusers.every(function(submitteduser) { // .some function tests see if elements pass // test defined function i.e. submitted user.id // match of goodusers ids if pass true else false // if return true .every returns true if of them // return false .every return false return goodusers.some(function(gooduser) { return gooduser.id === submitteduser.id; }); }); }; } module.exports = checkusersvalid;
Comments
Post a Comment