-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathscript.js
More file actions
177 lines (155 loc) · 5.16 KB
/
script.js
File metadata and controls
177 lines (155 loc) · 5.16 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const startOrder = () => {
// Step 1 - Welcome and introduction
alert(`Benvenuto! Welcome!
Click the button below to begin your order.`);
// Step 2 – Ask the user for their name
let userName = prompt("What is your name?");
// If no name is provided (= the user clicks Cancel), assign Anonynous
if (!userName) {
userName = "Anonymous";
}
// Step 3 – Welcome with their name + Ask for the type of food they want to order
// Using Do...while statement to prompt again if invalid value is entered
let foodType = "";
do {
let foodInput = prompt(`
Ciao ${userName} 👋
What would you like to eat today?
Enter a number:
1 – Pizza 🍕
2 – Pasta 🍝
3 – Salad 🥗
`);
// If user clicks cancel abort the order process
if (foodInput === null) {
alert("You cancelled the order. Goodbye!");
return;
}
// Parse the user's input
foodType = parseInt(foodInput);
// Assign the appropriate food type based on the user's input
if (foodType === 1) {
foodType = "Pizza";
} else if (foodType === 2) {
foodType = "Pasta";
} else if (foodType === 3) {
foodType = "Salad";
} else {
// Alert the user if an invalid selection was made
foodType = null;
alert(
"Sorry, that's not on the menu today. Please type 1, 2 or 3 to make your selection."
);
}
} while (foodType === null); // Keep asking until a valid option is chosen
// Step 4 - Ask for the specific type of food within the chosen category
let subFoodTypes = "";
let subFoodSelection = "";
// Set the sub-food options based on the selected food type
if (foodType === "Pizza") {
subFoodTypes = `Enter a number:
1 – Margaritha
2 – Capricciosa
3 – Hawaii`;
} else if (foodType === "Pasta") {
subFoodTypes = `Enter a number:
1 – Carbonara
2 – Vongole
3 – Lasagna`;
} else if (foodType === "Salad") {
subFoodTypes = `Enter a number:
1 – Caprese
2 – Insalata Russa
3 – Giardiniera`;
} else {
subFoodTypes = null;
}
// Use another do...while loop to get valid input for sub-food selection
do {
let subFoodInput = prompt(`
${foodType}, what an excellent choice!
What type would you like?
${subFoodTypes}
`);
// If user clicks cancel abort the order process
if (subFoodInput === null) {
alert("You cancelled the order. Goodbye!");
return;
}
// Parse the user's input
subFoodSelection = parseInt(subFoodInput);
// Assign the sub-food based on user's input
if (foodType === "Pizza") {
if (subFoodSelection === 1) {
subFoodSelection = "Margaritha";
} else if (subFoodSelection === 2) {
subFoodSelection = "Capricciosa";
} else if (subFoodSelection === 3) {
subFoodSelection = "Hawaii";
} else {
subFoodSelection = null;
}
} else if (foodType === "Pasta") {
if (subFoodSelection === 1) {
subFoodSelection = "Carbonara";
} else if (subFoodSelection === 2) {
subFoodSelection = "Spaghetti Alle Vongole";
} else if (subFoodSelection === 3) {
subFoodSelection = "Lasagne";
} else {
subFoodSelection = null;
}
} else if (foodType === "Salad") {
if (subFoodSelection === 1) {
subFoodSelection = "Caprese";
} else if (subFoodSelection === 2) {
subFoodSelection = "Insalata Russa";
} else if (subFoodSelection === 3) {
subFoodSelection = "Giardiniera";
} else {
subFoodSelection = null;
}
}
// Alert the user if they make an invalid sub-food selection
if (subFoodSelection === null) {
alert(
"We're all out of that for today I'm afraid. Please type 1, 2 or 3 to make your selection."
);
}
} while (subFoodSelection === null); // Keep asking until a valid selection is made
// Step 5 - Ask for the user's age to determine the portion size
let pizzaSize = prompt(
`Is the pizza for an adult or a child?
Please enter your age.`
);
// Step 6 - Confirm the order details and ask for confirmation
let orderConfirmation = "";
do {
if (Number(pizzaSize) >= 12) {
// Confirm adult-sized order if age is higher than 12
orderConfirmation = prompt(`Fantastico ${userName}!
Please confirm your order:
A delicious ${subFoodSelection} ${foodType} in adult size.
To confirm your order, type 'Yes'.
To decline, type anything else.
`);
} else if (Number(pizzaSize) < 12) {
// Confirm child-sized order if age is lower than 12
orderConfirmation = prompt(`Bellissimo ${userName}!
Please confirm your order:
A perfectly crafted ${subFoodSelection} ${foodType} in child size.
To confirm your order, type 'Yes'.
To decline, type anything else.
`);
} else {
// Prompt again if age input is invalid
pizzaSize = null;
}
} while (pizzaSize === null); // Keep asking until valid input is given
// Step 7 - Final message based on confirmation
if (orderConfirmation.toLowerCase() === "yes") {
alert(`Splendido ${userName}! Your order is on it's way.`);
} else {
alert("No worries, hope too see you soon again. Arrivederci!");
}
};