1
var findAllRecipes = function (recipes, ingredients, supplies) {2
const setOfIngredients = new Set(supplies);3
const recipesWithIngredients = new Map();4
const progress = new Map();5
//map recipes to it's ingredients and set each recipe to false in another map called progress6
for (let i = 0; i < recipes.length; i++) {7
recipesWithIngredients.set(recipes[i], ingredients[i]);8
progress.set(recipes[i], false);13
for (const rec of recipes) {14
if (recurse(rec, recipesWithIngredients, setOfIngredients, progress)) {16
setOfIngredients.add(rec);23
function recurse(rec, recipesWithIngredients, setOfIngredients, progress) {24
const currentIngredients = recipesWithIngredients.get(rec);25
for (const ingredient of currentIngredients) {26
if (setOfIngredients.has(rec)) continue;27
if (setOfIngredients.has(ingredient)) continue;28
else if (recipesWithIngredients.has(ingredient)) {29
if (progress.get(ingredient)) return false;30
progress.set(ingredient, true);31
let hasCycle = recurse(33
recipesWithIngredients,37
if (!hasCycle) return false;38
setOfIngredients.add(ingredient);