Created
June 5, 2020 18:26
-
-
Save marshallmurphy/1964f5d6ca8e57d53b7661347235f8df to your computer and use it in GitHub Desktop.
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
function firstComeFirstServe(takeAwayOrders, eatInOrders, servedOrders) { | |
// if any orders are not accounted for, return false | |
if ([...takeAwayOrders, ...eatInOrders].length != servedOrders.length) { | |
return false | |
} | |
servedOrders.forEach(order => { | |
if (takeAwayOrders[0] === order) { | |
// match! | |
takeAwayOrders.shift() | |
} else if (eatInOrders[0] === order) { | |
// match! | |
eatInOrders.shift() | |
} else { | |
// no match, not first-come first-serve | |
return false | |
} | |
}) | |
// check for remaining extra orders | |
return [...takeAwayOrders, ...eatInOrders].length == 0 ? true : false | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment