Meal maker project

I found it super hard too, felt completely lost, used all the hint boxes, saw the project walk through video, went to the forum and finally decided to take the lesson all over again. That helped a lot - I was able to do most of the project entirely without help (all though I did have to come back to the Forum for more help). After that, I did the project all over again and was able to complete it without any help. Super frustrating, but the reward in the end was great! Keep at it, surely you are not stupid! :smile:

1 Like
const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: []
  },
  get appetizers () {
    return this._courses.appetizers;
  },
  set appetizers (param) {
    this._courses.appatizers = param;
  },
  get mains () {
    return this._courses.mains;
  },
  set mains (param) {
    this._courses.mains = param;
  },
  get desserts () {
    return this._courses.desserts;
  },
  set desserts (param) {
    this._courses.desserts = param;
  },
  get courses () {
    return {
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts
    };
  },
  addDishToCourse(courseName, dishName, dishPrice) {
    let dish = {
      name: dishName,
      price: dishPrice
    };
   return this._courses[courseName].push(dish);
  },
  getRandomDishFromCourse(courseName){
    let dishes = this._courses[courseName];
    return dishes[Math.floor(Math.random() * dishes.length)];
  },
  generateRandomMeal(){
    let appetizers = this.getRandomDishFromCourse('appetizers');
    let mains = this.getRandomDishFromCourse('mains');
    let desserts = this.getRandomDishFromCourse('desserts');
    let totalPrice = `${appetizers.price + mains.price + desserts.price}$`;
    return `${appetizers}, ${mains}, ${desserts}, for ${totalPrice}`
  }
};

menu.addDishToCourse("appetizers", "bread", 5);
menu.addDishToCourse("appetizers", "bean", 4);
menu.addDishToCourse("mains", "pasta", 10);
menu.addDishToCourse("mains", "pizza", 12);
menu.addDishToCourse("desserts", "snack", 3);
menu.addDishToCourse("desserts", "icecream", 5);

let meal = menu.generateRandomMeal()

console.log(meal);

the result is
[object Object], [object Object], [object Object], for 22$

I don’t know what’s wrong with this…

here:

return `${appetizers}, ${mains}, ${desserts}, for ${totalPrice}`

you return objects (appetizers, mains and desserts), not the price property of these objects. The output ([object object] tells you as much

1 Like
const menu = {
  _courses:{
    appetizers:[],
    mains:[],
    desserts:[],
  
  get appetizers() {

  },
  set appetizers(appetizerIn) {
    this._course.appetizers = appetizerIn;


  },

  get mains() {

  },
  set mains(mainsIn) {
this._course.mains = mainIn;
  },
  get desserts() {

  },
  set desserts(dessertsIn){
this._course.desserts = dessertsIn;
  },
  
  
  },
  get courses() {
    return {appetizers: this.appetizers,
            mains: this.mains,
            desserts: this.desserts,
    };

  },
  
 addDishToCourse (courseName,dishName,dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice}
    return this._courses[courseName].push(dish);
  },

 getRandomDishFromCourse (courseName) {
    const dishes = this._courses[courseName];
    const randomIndex = math.floor(math.random() * dishes.length) 
return dishes[randomIndex]
  },

  generateRandomMeal() {
    const appetizer = this.getRandomDishFromCourse('appetizers')
    const mains = this.getRandomDishFromCourse('mains')
    const dessert = this.getRandomDishFromCourse('dessert')
    const totalPrice = appetizer.price+mains.price+dessert.price
    return `this is ${appetizer.name}, ${mains.name}, ${dessert.name} and the total price is ${totalPrice}`
  },

}


menu.addDishToCourse('appetizers','Hummus',5.50 )
menu.addDishToCourse('appetizers','Salad',3.50 )
menu.addDishToCourse('appetizers','Rolls',4.50 )

menu.addDishToCourse('mains','Pizza',12.99 )
menu.addDishToCourse('mains','Noddles',8.99 )
menu.addDishToCourse('mains','Burger',10.50 )

menu.addDishToCourse('desserts','Icecream',5.50 )
menu.addDishToCourse('desserts','Coffee',3.50 )
menu.addDishToCourse('desserts','Gelato',6.50 )

const meal = menu.generateRandomMeal()
console.log(meal)

Error message
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:43
return this._courses[courseName].push(dish);

Would really appreciate if someone could help.

we should inspect the state of the program:

 addDishToCourse (courseName,dishName,dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice}
    console.log(Object.keys(this._courses), courseName, this._courses[courseName]);
    return this._courses[courseName].push(dish);
  },

we can see (using .log()) that the property exists, but we get undefined. Which seems to be a problem with your getter

your getter doesn’t return anything.

Sidebar comment: Since the method is acting as a setter, it needs no return keyword.

Hi, thanks for replying. Is there anything in particular that I’m getting wrong. I’ve tried to follow the guided videos too and my code still doesn’t work. Ive fixed some of the wording errors from the code that i just posted though.

Any idea why I may be getting this error?
image

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  get appetizers () {
    this._courses.appetizers;
  },
  get mains () {
    this._courses.mains;
  },
  get desserts () {
    this._courses.desserts;
  },
  set appetizers(appetizer) {
    this._courses.appetizers = appetizers;
  },
  set mains(main) {
    this._courses.mains = mains;
  },
  set desserts(dessert) {
    this._courses.desserts = desserts;
  }, 
  get courses() {
   return {
    appetizers: this.appetizers,
    mains: this.mains,
    desserts: this.desserts,
   };
  },
  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    return this._courses[courseName].push(dish);
  },
  getRandomDishFromCourse(courseName) {
    const dishes = this._courses[courseName];
    const randomIndex = Math.floor(Math.random() * dishes.length);
    return dishes[randomIndex];
  },
generateRandomMeal() {
  const appetizer = this.getRandomDishFromCourse('appetizers');
  const main = this.getRandomDishFromCourse('mains');
  const dessert = this.getRandomDishFromCourse('desserts');
  const totalPrice = appetizer.price + main.price + dessert.price;

  return `Your meal of ${appetizer}, ${main}, and ${dessert} comes to the total of ${totalPrice}.`;
}
}; 

menu.addDishToCourse('appetizers', 'ceasar salad', 4.50);
menu.addDishToCourse('appetizers', 'bacon bits', 4.50);
menu.addDishToCourse('appetizers', 'wings', 9.50);

menu.addDishToCourse('mains', 'lobster', 45.50);
menu.addDishToCourse('mains', 'steak', 55.50);
menu.addDishToCourse('mains', 'chicken', 29.95);

menu.addDishToCourse('dessert', 'cheesecake', 9.50);
menu.addDishToCourse('dessert', 'ice cream', 7.50);
menu.addDishToCourse('dessert', 'creamcake', 9.50);  

const meal = menu.generateRandomMeal();

console.log(meal)
 

We wouldn’t return from this method since the setting action takes place right there. Nothing is expected by the caller.

Comment that line, for now and add a console.log() to the method that logs out,

this._courses[courseName]

Chance are the lines that are causing the error are the three with 'dessert' in them. The property is declared as, 'desserts'.

The result of my code returns only the string not any of the values:
Your meal is {appetizer.name}, {main.name}, {dessert.name}. The price is {totalPrice}.

Where did i go wrong?

const menu = {

_courses: {

appetizers: [],

mains: [],

desserts: [],

},

get appetizers() {

return this._courses.appetizers;

},

set appetizers(appetizers) {

this._courses.appetizers = appetizers;

},

get mains() {

return this._courses.mains;

},

set mains(mains) {

this._courses.mains = mains;

},

get desserts() {

return this._courses.desserts;

},

set desserts(desserts) {

this._courses.desserts = desserts;

},

get courses() {

return {

  appetizers: this.appetizers,

  mains: this.mains,

  desserts: this.desserts,

};

},

addDishToCourse(courseName, dishName, dishPrice) {

    const dish = {

      name : dishName,

      price : dishPrice

    };

    return this._courses[courseName].push(dish);

},

getRandomDishFromCourse(courseName) {

  const dishes = this._courses[courseName];

  const randomIndex = Math.floor(Math.random()*dishes.length);

  return dishes[randomIndex];

},

generateRandomMeal() {

  const appetizer = this.getRandomDishFromCourse('appetizers');

  const main = this.getRandomDishFromCourse('mains');

  const dessert = this.getRandomDishFromCourse('desserts');

  const totalPrice = appetizer.price + main.price + dessert.price;

  return 'Your meal is ${appetizer.name}, ${main.name}, ${dessert.name}. The price is ${totalPrice}.';

  

}

}

menu.addDishToCourse(‘appetizers’, ‘Caesar Salad’, 4.35)

menu.addDishToCourse(‘appetizers’, ‘Mozzarella Sticks’, 5.00)

menu.addDishToCourse(‘appetizers’, ‘Calamari’, 6.67)

menu.addDishToCourse(‘mains’, ‘Cheeseburger’, 7.89)

menu.addDishToCourse(‘mains’, ‘Steak’, 99.89)

menu.addDishToCourse(‘mains’, ‘Chicken Wrap’, 8.67)

menu.addDishToCourse(‘desserts’, ‘Cake’, 7.88)

menu.addDishToCourse(‘desserts’, ‘Ice Cream’, 1.89)

menu.addDishToCourse(‘desserts’, ‘Cookies’, 2.57)

let meal = menu.generateRandomMeal();

console.log(meal);

to use template literals/template strings, the string needs to be enclosed in back-ticks/grave-accent, see documentation:

Template literals (Template strings) - JavaScript | MDN

Thank you! I was going nuts trying to figure out why it wasnt working.

Hi everyone - I am getting a TypeError on line 42. “TypeError: Cannot read property ‘length’ of undefined
at Object.getRandomDishFromCourse”

can anyone help with this? Thank you

const menu = { _courses : { appetizers: [], mains: [], desserts: [] }, get appetizers() { return this._courses.appetizers; }, get mains() { return this._courses.mains; }, get desserts() { return this._courses.desserts; }, set appetizers(appetizers) { this._courses.appetizers = appetizers; }, set mains(mains) { this._courses.mains = mains; }, set desserts(desserts) { this._courses.desserts = desserts }, get courses() { return { appetizers: this.appetizers, mains: this.mains, desserts: this.desserts, // or use return _courses } }, addDishToCourse(courseName, dishName, dishPrice) { const dish = { name: courseName, price: dishPrice, }; return this._courses[courseName].push(dish); }, getRandomDishFromCourse(courseName) { const dishes = this._courses[courseName]; const randomIndex = Math.floor(Math.random() * dishes.length); return dishes[randomIndex]; }, generateRandomMeal() { const appetizer = this.getRandomDishFromCourse('appetizer'); const main = this.getRandomDishFromCourse('main'); const dessert = this.getRandomDishFromCourse('dessert'); const totalPrice = appetizer.price + main.price + dessert.price; return `Your meal is ${appetizer.name}, ${main.name}, and ${dessert.name}, and the total price is ${totalPrice}.` } }; menu.addDishToCourse('appetizers', 'salad', 1.00); menu.addDishToCourse('appetizers', 'wings', 2.00); menu.addDishToCourse('appetizers', 'mushrooms', 3.00); menu.addDishToCourse('mains', 'pizza', 4.00); menu.addDishToCourse('mains', 'steak', 5.00); menu.addDishToCourse('mains', 'chicken', 6.00); menu.addDishToCourse('desserts', 'cake', 7.00); menu.addDishToCourse('desserts', 'brownie', 8.00); menu.addDishToCourse('desserts', 'ice cream', 9.00); const meal = menu.generateRandomMeal(); console.log(meal)

I found some problems with the names of some variables:

in the function addDishToCourse,
in the dish object,
name: courseName,
should be
name: dishName,

In the function generateRandomMeal
const appetizer = this.getRandomDishFromCourse('appetizer');
should be
const appetizer = this.getRandomDishFromCourse('appetizers');
because appetizers is the parameter you used earlier in the menu object.

similarly for main and dessert at the end of the next lines.

The reason for the error is that it’s trying to read the length of
menu._courses['appetizer'] inside of the getRandomDishFromCourse function,
but menu._courses['appetizer'] does not exist,
so its length does not exist.
What you actually want is menu._courses['appetizers'].

Thank you very much :slight_smile:

Hello everyone,

Hello everyone, and then code dump? What about it?