Meal maker difficulties! 5th Attempt

Hi all,

I am really struggling with this project. I have tried multiple times to try and get a hold on this but still no luck!!!

I have tried reviewing all the guides, revisiting the lessons and even used the walk-though video (which was no help what so ever!!!)

i have even tried doing it in VSC so that i can track were each element has an effect on the rest of the code, and cannot see me error using that method either!

Can someone please have a look through my code and see where I have gone wrong!

(project URL: https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-learn-javascript-syntax-objects/projects/meal-maker )

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
    };
  },
  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice
    };
    return this._courses[courseName].push(dishName);
  },
  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} and ${dessert.name}, and the total price is ${totalPrice}`
  },
};


menu.addDishToCourse('appetizers', 'salad', 4.00);
menu.addDishToCourse('appetizer', 'wings', 4.50 );
menu.addDishToCourse('appetizer', 'fries', 5.00);

menu.addDishToCourse('mains', 'steak', 10.25);
menu.addDishToCourse('mains', 'salmon', 7.75);
menu.addDishToCourse('mains', 'tofu', 11.20);

menu.addDishToCourse('desserts', 'ice cream', 3.00);
menu.addDishToCourse('desserts', 'coffee', 3.00);
menu.addDishToCourse('desserts', 'cake', 3.25);


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

Hi you’re very close, you just have a spelling mistake.

A good thing to do to debug is to see exactly what part of your code is causing your error and check ALL the conditions in the space that happens right before it by logging them (even the obvious ones):

this._courses, courseName, dishName etc.

Hi,

thank you for helping me out!!

I still cant see where this spelling mistake is, think i have looked over this so many times I’ve gone blind to it all!!

thanks

Try console.log on courseName right before the mistake.

A little extra tip: I think you got a little confused with plural and singular.

1 Like

I’ll be honest, I am struggling in understanding how each section of the code has an effect of the overall output.

Would someone be able to give me a break down of the code so that I can follow how each part interacts with the rest?

thanks!

(And i still can not find that damned mistake!)

Ok here is the spoiler:
You’ve done it right once.

And once you’ve eliminated that, you’ll come across another problem.
I’ll try to go through step by step:

I guess you don’t have difficulties with that: Inside your menu object you add a key which has another object as its value: _courses. Inside your _courses object you have key-value-pairs: The key is the name of the course and the value is an empty array, which will be filled with dishes. Again objects with the dishName and dishPrice by your method addDishToCourse.

We can skip that. You don’t use the getters and setters in the exercise and there might be someone more qualified to explain why these are best practice for coding.

When you call this method (with non misspelled arguments, the key is appetizers not ‘appetizer’), you fill the empty arrays of _courses with objects (with the method push()). You don’t need to return anything from that function as you do here. You can change that line to:

this._courses[courseName].push(dishName);

That method addDishToCourse takes three parameters (the first you misspelled in your function call) this._courses[courseName]defines to which array the dish should be pushed. Then you pass dishName and dishPrice and store them in the const dish. But you just push the dishName to the _course object. Try to fix that.

This function just makes sense when there are already dishes in your _courses object. There should be several dishes in each course, but you just want to fetch one. That’s why you generate a random index from 0 to dishes.length. From that method you return a single dish (from each course) to the calling method, which is:

And here we have another problem: You get the random dishes (and their prices once you fixed that) from the getRandomDishFromCourse method and store the values to the const appetizer, const main and const desserts, but you don’t return them in your literals. Instead you return object keys (which don’t exist). You can try to fix that, too. I hope, that explanation helps you doing that.

2 Likes