if (typeof window === "undefined") { if ( !globalThis.stocks && !globalThis.recipes && !globalThis.globalThis.sleep ) { const { stocks, recipes } = require("./data"); const { sleep } = require("./sleep"); globalThis.stocks = stocks; globalThis.recipes = recipes; globalThis.globalThis.sleep = sleep; } } if (typeof window === "undefined") { module.exports = { order, }; } /* * * Write your code below these lines * */ async function end_of_order(recipeName, wait) { if (wait) { await globalThis.sleep(1); } console.log("All ingredients have been prepared"); await globalThis.sleep(2); console.log(`Cooking ${recipeName}`); await globalThis.sleep(5); console.log(`Delivering ${recipeName}`); return null; } function check_and_update_ingredient(recipeName, ingredient, quantity) { if (globalThis.stocks[ingredient] < quantity) { console.log( `Not enough ingredients for ${recipeName} because there is no more ${ingredient}`, ); return false; } globalThis.stocks[ingredient] -= quantity; return true; } function check_and_update_ingredients(recipeName, ingredients) { for (const ingredient in ingredients) { const quantity = ingredients[ingredient]; if (!check_and_update_ingredient(recipeName, ingredient, quantity)) { return false; } } return true; } async function order(recipeName) { console.log(`Ordering ${recipeName}`); if ( !(recipeName in globalThis.recipes.pizza) && !(recipeName in globalThis.recipes.burger) ) { console.log(`Recipe ${recipeName} does not exist`); return; } await globalThis.sleep(2); console.log(`Production has started for ${recipeName}`); await globalThis.sleep(1); if (recipeName in globalThis.recipes.burger) { const burger = globalThis.recipes.burger[recipeName]; for (const ingredient in burger) { console.log(`Preparing ${ingredient}`); if ( !check_and_update_ingredient( recipeName, ingredient, burger[ingredient], ) ) { return; } await globalThis.sleep(1); } return await end_of_order(recipeName, false); } else if (recipeName in globalThis.recipes.pizza) { const pizza = globalThis.recipes.pizza[recipeName]; console.log(`Preparing ${pizza.sauce}`); if (!check_and_update_ingredient(recipeName, pizza.sauce, 1)) { return; } await globalThis.sleep(1); console.log(`Preparing ${Object.keys(pizza.toppings).join(", ")}`); if (!check_and_update_ingredients(recipeName, pizza.toppings)) { return; } await globalThis.sleep(1); console.log(`Preparing ${Object.keys(pizza.cheese).join(", ")}`); if (!check_and_update_ingredients(recipeName, pizza.cheese)) { return; } return await end_of_order(recipeName, true); } } if (typeof window === "undefined") { module.exports = { order, }; }