Skip to content

Instantly share code, notes, and snippets.

@nichoth
Last active October 7, 2024 18:03
Show Gist options
  • Save nichoth/dd132fe1c7ce958619faf51909e93915 to your computer and use it in GitHub Desktop.
Save nichoth/dd132fe1c7ce958619faf51909e93915 to your computer and use it in GitHub Desktop.
Find how many ingredients you need to buy
//
// I always like an opportunity to use `Set` in JS.
// There should be `difference` and `intersection` operators
// in new environments, but my Node didn't have them,
// so we're doing it slightly more laboriously.
// (`node -v` = 20.16.0)
//
const recipe = ['eggs', 'flour', 'sugar', 'butter']
const pantry = ['sugar', 'butter', 'milk']
const both = new Set([...recipe, ...pantry])
Array.from(both).length - pantry.length
// => 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment