1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
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,
};
}
|