Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Hi Amy, my notes and a working solution below:
- 1) first let's start with indentation:
- when we open a new object or function, we indent two spaces in.
- See if you can identify that indentation pattern below
- 2) Next, in your addDishToCourse method, you create an dish object
- based on the dish parameters that come into the method. So you
- use the syntax `property: value`. Here it's a little confusing that
- the property and value have the same name:
- var dish = {
- courseName: courseName,
- dishName: dishName,
- dishPrice: dishPrice
- }
- it just means the dish object will have courseName, dishName and dishPrice
- properties set to the respectively named parameters that enter the function.
- 3) Now let's look at the getRandomDishFromCourse method. In your code,
- you weren't returning a value on the last line of your method, so your
- method was returning "undefined." Since getRandomDishFromCourse is called by another method, namely, generateRandomMeal, this creates problems.
- In my implementation below, getRandomDishFromCourse returns a random dish from
- whatever course array is passed into the method.
- 4) in generateRandomMeal, I tweaked some of your variable naming conventions so
- that they're all named consistently and in accord with the logic of the method itself
- 5) outside of the menu object, you were passing multiple parameters into the same
- addDishToCourse method. Unfortunately you have to pass them in one at a time,
- or use a loop. So I passed them one at a time below.
- */
- var menu = {
- courses: {
- appetizers: [],
- mains: [],
- desserts: [],
- },
- addDishToCourse: function (courseName, dishName, dishPrice) {
- var dish = {
- courseName: courseName,
- dishName: dishName,
- dishPrice: dishPrice
- }
- this.courses[courseName].push(dish);
- },
- getRandomDishFromCourse: function (courseName) {
- var dishes = this.courses[courseName];
- var randomIndex = Math.floor(Math.random() * dishes.length);
- return dishes[randomIndex];
- },
- generateRandomMeal: function() {
- var appetizer = this.getRandomDishFromCourse('appetizers');
- var main = this.getRandomDishFromCourse('mains');
- var dessert = this.getRandomDishFromCourse('desserts');
- var totalPrice = appetizer.dishPrice + main.dishPrice + dessert.dishPrice;
- return ('Appetizer: ' + appetizer.dishName + '\n' +
- 'Main Course: ' + main.dishName + '\n' +
- 'Dessert: ' + dessert.dishName + '\n' +
- 'The total price is $' +
- totalPrice + '.'
- );
- }
- }
- menu.addDishToCourse('appetizers', 'Shrimp Cocktail', 13.50);
- menu.addDishToCourse('appetizers', 'Lobster Bisque', 7.00);
- menu.addDishToCourse('appetizers', 'Ceasar Salad', 4.25);
- menu.addDishToCourse('mains', 'Chicken with Rice', 14.25);
- menu.addDishToCourse('mains', 'Shrimp Scampi with Pasta', 23.50);
- menu.addDishToCourse('mains', 'Lobster Tail', 27.00);
- menu.addDishToCourse('desserts', 'Rice Pudding', 2.25);
- menu.addDishToCourse('desserts', 'Pie ala Mode', 3.50);
- menu.addDishToCourse('desserts', 'Brownie Delight', 4.00);
- //
- var meal = menu.generateRandomMeal();
- console.log(meal);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement