Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. /*
  2. Hi Amy, my notes and a working solution below:
  3.  
  4.  
  5. 1) first let's start with indentation:
  6. when we open a new object or function, we indent two spaces in.
  7. See if you can identify that indentation pattern below
  8.  
  9. 2) Next, in your addDishToCourse method, you create an dish object
  10. based on the dish parameters that come into the method. So you
  11. use the syntax `property: value`. Here it's a little confusing that
  12. the property and value have the same name:
  13.  
  14. var dish = {
  15. courseName: courseName,
  16. dishName: dishName,
  17. dishPrice: dishPrice
  18. }
  19.  
  20. it just means the dish object will have courseName, dishName and dishPrice
  21. properties set to the respectively named parameters that enter the function.
  22.  
  23. 3) Now let's look at the getRandomDishFromCourse method. In your code,
  24. you weren't returning a value on the last line of your method, so your
  25. method was returning "undefined." Since getRandomDishFromCourse is called by another method, namely, generateRandomMeal, this creates problems.
  26.  
  27. In my implementation below, getRandomDishFromCourse returns a random dish from
  28. whatever course array is passed into the method.
  29.  
  30. 4) in generateRandomMeal, I tweaked some of your variable naming conventions so
  31. that they're all named consistently and in accord with the logic of the method itself
  32.  
  33. 5) outside of the menu object, you were passing multiple parameters into the same
  34. addDishToCourse method. Unfortunately you have to pass them in one at a time,
  35. or use a loop. So I passed them one at a time below.
  36.  
  37. */
  38.  
  39. var menu = {
  40. courses: {
  41. appetizers: [],
  42. mains: [],
  43. desserts: [],
  44. },
  45. addDishToCourse: function (courseName, dishName, dishPrice) {
  46. var dish = {
  47. courseName: courseName,
  48. dishName: dishName,
  49. dishPrice: dishPrice
  50. }
  51. this.courses[courseName].push(dish);
  52. },
  53. getRandomDishFromCourse: function (courseName) {
  54. var dishes = this.courses[courseName];
  55. var randomIndex = Math.floor(Math.random() * dishes.length);
  56. return dishes[randomIndex];
  57. },
  58. generateRandomMeal: function() {
  59. var appetizer = this.getRandomDishFromCourse('appetizers');
  60. var main = this.getRandomDishFromCourse('mains');
  61. var dessert = this.getRandomDishFromCourse('desserts');
  62. var totalPrice = appetizer.dishPrice + main.dishPrice + dessert.dishPrice;
  63. return ('Appetizer: ' + appetizer.dishName + '\n' +
  64. 'Main Course: ' + main.dishName + '\n' +
  65. 'Dessert: ' + dessert.dishName + '\n' +
  66. 'The total price is $' +
  67. totalPrice + '.'
  68. );
  69. }
  70. }
  71. menu.addDishToCourse('appetizers', 'Shrimp Cocktail', 13.50);
  72. menu.addDishToCourse('appetizers', 'Lobster Bisque', 7.00);
  73. menu.addDishToCourse('appetizers', 'Ceasar Salad', 4.25);
  74. menu.addDishToCourse('mains', 'Chicken with Rice', 14.25);
  75. menu.addDishToCourse('mains', 'Shrimp Scampi with Pasta', 23.50);
  76. menu.addDishToCourse('mains', 'Lobster Tail', 27.00);
  77. menu.addDishToCourse('desserts', 'Rice Pudding', 2.25);
  78. menu.addDishToCourse('desserts', 'Pie ala Mode', 3.50);
  79. menu.addDishToCourse('desserts', 'Brownie Delight', 4.00);
  80. //
  81.  
  82. var meal = menu.generateRandomMeal();
  83. console.log(meal);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement