-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildMeal1.js
63 lines (57 loc) · 2.59 KB
/
buildMeal1.js
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
/*Built a 'menu' object to contain a series
of nested objects. The 'courses' object contains
three empty arrays, one for each course.*/
let menu = {
courses: {
appetizers: [],
mains: [],
desserts: [],
},
/*The following method accepts 3 parameters: course, dish name, and
price. It assigns a variable, 'dish', that holds an object containing
name & price. Based on the courseName, the contents of 'dish' are pushed
into the appropriate array above. */
addDishToCourse(courseName, dishName, dishPrice) {
let dish = {
name: dishName,
price: dishPrice,
};
menu.courses[courseName].push(dish);
},
/*The following method accepts one parameter: the name of the course.
The 'dishes' variable contains the array of all the dishes for that course. 'Index' is a randomly-generated value between 0 and the number of dishes
minus 1; this index value is used to select a key-value pair (name:price)
from the dishes array.*/
getRandomDishFromCourse(courseName) {
let dishes = menu.courses[courseName];
let index = Math.floor(Math.random() * dishes.length);
return dishes[index];
},
/*This method generates random appetizer, main, and dessert name:price
pairs using the RandomDish method above. It adds the price of each and
returns a statement listing the meal and total price.*/
generateRandomMeal() {
let appetizer = menu.getRandomDishFromCourse("appetizers");
let main = menu.getRandomDishFromCourse("mains");
let dessert = menu.getRandomDishFromCourse("desserts");
let totalPrice = appetizer.price + main.price + dessert.price;
return `Your appetizer (${appetizer.name}), main course (${main.name}), and dessert (${dessert.name}) will cost $${totalPrice} plus tax. A 20% gratuity is appreciated. `;
},
};
/*These method calls add items to the menu!*/
menu.addDishToCourse("appetizers", "samosa", 3);
menu.addDishToCourse("appetizers", "lentil soup", 2.5);
menu.addDishToCourse("appetizers", "papaya salad", 2);
menu.addDishToCourse("appetizers", "chaat", 2.75);
menu.addDishToCourse("mains", "dahl", 7);
menu.addDishToCourse("mains", "chicken tikka", 10);
menu.addDishToCourse("mains", "red curry", 9);
menu.addDishToCourse("mains", "tofu masala", 8);
menu.addDishToCourse("desserts", "mango sticky rice", 3.5);
menu.addDishToCourse("desserts", "chocolate cake", 5);
menu.addDishToCourse("desserts", "watermelon", 3.5);
menu.addDishToCourse("desserts", "green tea ice cream", 2);
/*This call assigns the output of the RandomMeal method to the variable 'meal'
and then logs the output to the console.*/
let meal = menu.generateRandomMeal();
console.log(meal);