Meal Maker Mess Ups


#1

https://www.codecademy.com/courses/learn-javascript-objects/projects/meal-maker?r=master

Hello, when running my code here. I receive getRandomDishFromCourse() is not defined. This project is actually a little tough. I’m glad though, I want to learn. Thanks in advance to everyone. Here is my code:

let menu = {
  _courses:
  {
  _appetizers: [],
  _mains: [],
  _desserts: [],
  //appetizer setter 
  set appetizers(appetizer){
      this._appetizers.push(appetizer);
  },  
  //appetizer get
  get appetizers(){
  	return this._appetizers;
  },
  //Main setter  
  set mains(main){
     this._mains.push(main);
  },
  //Main getter
  get mains(){
   return this._mains;
  },
  //Desserts set
  set desserts(dessert) {
      this._desserts.push(dessert);
  },
  //Desserts get
  get desserts(){
    return this._desserts;
  }
    
},
  
  get courses(){
   return {
     appetizers: this._courses.appetizers,
     mains: this._courses.mains,
     desserts: this._courses.desserts,
   }
  },
  
  addDishToCourse (courseName, dishName, dishPrice){
    const dish = {
      _name: [],
      _price: [], 
      
      set name(dishName){
        if(typeof dishName === 'string'){
          this._name = dishName;
          console.log(`Great. ${dishName} has been added.`);
        }else{
          console.log(`${dishName} is not a valid choice.`);
        }
      },
      
      set price(dishPrice){
        if(typeof dishPrice === 'number'){
          this._price = dishPrice;
          console.log(`Great. ${dishPrice} has been added ...`);
        }else{
          console.log(`${dishPrice} is not a dollar amount.`);
        }
      },
      
     
      
        }
      },
  
  getRandomDishFromCourse(courseName) {
   
      const dishes = this._courses[courseName];
      const randomIndex = Math.floor(Math.random() * dishes.length);
    
  },
  
  generateRandomMeal() {
  const appetizer = getRandomDishFromCourse('appetizers');
  const main = getRandomDishFromCourse('mains');
  const dessert = getRandomDishFromCourse('desserts');
    
  const totalPrice = appetizer.price + dessert.price + main.price;
    return `Your meal is ${appetizer.name}, ${main.name}, and your dessert is ${dessert.name}. Your total is $${totalPrice}.`;
//Should I use ${totalPice}.tofixed here? If so, why?    
}
    };

menu.addDishToCourse('appetizers','Pot Stickers', 6.25);
menu.addDishToCourse('appetizers','Wanton Tacos', 7.25);

menu.addDishToCourse('main','Baby Back Ribs', 21.25);
menu.addDishToCourse('main ','Garlic Crusted Chicken', 12.50);

menu.addDishToCourse('desserts','Blondie', 8.30);
menu.addDishToCourse('desserts','Brownie Sundae', 9.15);

let meal = menu.generateRandomMeal();

console.log(meal);

#2

Sorry about my sloppy code and presentation.


#3

you can use markup to make your code look better on the forum:

i now did this for you

to call another method within an object requires the this keyword, so it should be:

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

same applies for main and dessert, when you also call getRandomDishFromCourse

if we need to access properties, we also need the this keyword, remember?


#4

Ah I see. Let me go back and revisit this. Thank you so much for such a quick response!


#5

the this keyword is tricky, but we need it to access properties within the object. You can always revisit old exercises if you need a syntax refresher, and MDN (Mozilla developers network) has always good documentation


#6

I very well may have to revisit the object lesson several times. Trying to wrap my mind around what’s actually happening gets a bit tricky at times for me. I actually forgot for a moment that getRandomDishCourse() was in the menu object. I’m still very new to this, but thank you for helping so quickly and for helping me to format my post correctly


#7

I mostly write code in python, not in JS. And given JS is used with the DOM (webpages), objects in JS are implement different, thankfully MDN has a page about this:

but to fully understand the documentation, you need to understand that JS is the only language we have for front-end web development. So JS is closely entangled with html and css (the languages in which we can build web-pages)


#8

JS is actually my first object-oriented language I’m attempting to pickup. So once a program puts objects within objects and so on I start to get a bit lost. I don’t know if its just readability on my part or inexperience, but that’s when it really gets challenging. I will remember however, that anytime I’m working within an object I need to use this to access any properties. Another question I have though is in regards to this line here:

    const dish = {
      _name: [],
      _price: [], 
      
      set name(dishName){
        if(typeof dishName === 'string'){
          this._name = dishName;
          console.log(`Great. ${dishName} has been added.`);
        }else{
          console.log(`${dishName} is not a valid choice.`);
        }
      },
      
      set price(dishPrice){
        if(typeof dishPrice === 'number'){
          this._price = dishPrice;
          console.log(`Great. ${dishPrice} has been added ...`);
        }else{
          console.log(`${dishPrice} is not a dollar amount.`);
        }
      },
        }
      },
this._courses[courseName].push(name);```

I'm having a hard time understanding how to push a parameter into an array properly. I may not understand when to use `push()`

#9

i can’t find the problem, but the problem doesn’t have to do with push()

if i run your code, i don’t see the effect if this line:

console.log(`Great. ${dishName} has been added.`);

there is never a message that the dishes are added, and i don’t believe dishes are added, but i am unsure why


#10

Thanks Stetim94, I moved on to the next project to try to get a better understanding of what is going on.

I did find out that here:

this._courses[courseName].push(name); was outside of the addDishToCourse() function. So I moved it back into the function. Now when I run the program I get this:

Your meal is undefined, undefined, and your dessert is undefined. Your total is $NaN..

All I can say is …Hello Darkness my old friend.....


#11

but the problem seems to be that the dishes are never properly added. If the dishes where added, Great. ${dishName} has been added. should show up in the output window, where then dishName is the actual name of the dish


#12

Exactly, I think I need to redo this project and start from the beginning. I may be able to catch what I missed. The addDishToCourse() function should work. I wonder if the parameter courseName is mapping properly?


#13

looking at the hint:

const menu = {
  ...
  addDishToCourse (courseName, ...) {
    const dish = {
      ...
    };

    this._courses[courseName].push(dish); // also try using your setter method!
  }
};

this._courses[courseName].push(dish) is placed outside dish dictionary, this looks different from your code


#14

Now that I look at it, I don’t event think I needed to include the if statement. It just makes the code unreadable.


#15

i have been fiddling with your code:

let menu = {
  _courses:
  {
  _appetizers: [],
  _mains: [],
  _desserts: [],
  //appetizer setter 
  set appetizers(appetizer){
      this._appetizers.push(appetizer);
  },  
  //appetizer get
  get appetizers(){
  	return this._appetizers;
  },
  //Main setter  
  set mains(main){
     this._mains.push(main);
  },
  //Main getter
  get mains(){
   return this._mains;
  },
  //Desserts set
  set desserts(dessert) {
      this._desserts.push(dessert);
  },
  //Desserts get
  get desserts(){
    return this._desserts;
  }
    
},
  
  get courses(){
   return {
     appetizers: this._courses.appetizers,
     mains: this._courses.mains,
     desserts: this._courses.desserts,
   }
  },
  
  addDishToCourse (courseName, dishName, dishPrice){
    
    const dish = {
      _name: [],
      _price: [], 
      
      set name(dishName){
        if(typeof dishName === 'string'){
          this._name = dishName;
          console.log(`Great. ${dishName} has been added.`);
        }else{
          console.log(`${dishName} is not a valid choice.`);
        }
      },
      
      set price(dishPrice){
        if(typeof dishPrice === 'number'){
          this._price = dishPrice;
          console.log(`Great. ${dishPrice} has been added ...`);
        }else{
          console.log(`${dishPrice} is not a dollar amount.`);
        }
        
  		       
      },
      
  	}; /* closes dish dictionary */
    
    if (this._courses[courseName] == undefined){
      console.log('oops')
    } else {	
      console.log(dish)
    	this._courses[courseName].push(dish);
    }
      },
  

  
  getRandomDishFromCourse(courseName) {
   
      const dishes = this._courses[courseName];
      const randomIndex = Math.floor(Math.random() * dishes.length);
    
  },
  
  generateRandomMeal() {
  const appetizer = this.getRandomDishFromCourse('appetizers');
  const main = this.getRandomDishFromCourse('mains');
  const dessert = this. getRandomDishFromCourse('desserts');
    
  const totalPrice = this.addDishToCourse('appetizers') + this.addDishToCourse('main') + this.addDishToCourse('desserts');
    return `Your meal is ${appetizer}, ${main}, and your dessert is ${dessert}. Your total is $${totalPrice}.`;
//Should I use ${totalPice}.tofixed here? If so, why?    
}
    };

menu.addDishToCourse('appetizers','Pot Stickers', 6.25);
menu.addDishToCourse('appetizers','Wanton Tacos', 7.25);

menu.addDishToCourse('mains','Baby Back Ribs', 21.25);
menu.addDishToCourse('mains ','Garlic Crusted Chicken', 12.50);

menu.addDishToCourse('desserts','Blondie', 8.30);
menu.addDishToCourse('desserts','Brownie Sundae', 9.15);

let meal = menu.generateRandomMeal();

console.log(meal);

what is worrying me is the output caused by this line:

console.log(dish)

the output caused by this line is:

{ _name: [], _price: [], name: [Setter], price: [Setter] }
{ _name: [], _price: [], name: [Setter], price: [Setter] }
{ _name: [], _price: [], name: [Setter], price: [Setter] }
oops
{ _name: [], _price: [], name: [Setter], price: [Setter] }
{ _name: [], _price: [], name: [Setter], price: [Setter] }
{ _name: [], _price: [], name: [Setter], price: [Setter] }
oops
{ _name: [], _price: [], name: [Setter], price: [Setter] }

for some reason our dish dictionaries has 4 entries, that is not suppose to be, it should have 2 entries? And we get some weird undefined s, not sure where they are coming from

i also changed:


menu.addDishToCourse('mains','Baby Back Ribs', 21.25);
menu.addDishToCourse('mains ','Garlic Crusted Chicken', 12.50);

i thought it might need to be mains, given that is the name of our array as well


#16

wow, lol. I have no idea how that happened.

Shouldn’t there be a getter for _name prior to me setting it to this.addDishToCourse._name = dishName?

How _name: made its way in there is a mystery.


#17

i fixed part of it:

let menu = {
  _courses:
  {
  _appetizers: [],
  _mains: [],
  _desserts: [],
  //appetizer setter 
  set appetizers(appetizer){
      this._appetizers.push(appetizer);
  },  
  //appetizer get
  get appetizers(){
  	return this._appetizers;
  },
  //Main setter  
  set mains(main){
     this._mains.push(main);
  },
  //Main getter
  get mains(){
   return this._mains;
  },
  //Desserts set
  set desserts(dessert) {
      this._desserts.push(dessert);
  },
  //Desserts get
  get desserts(){
    return this._desserts;
  }
    
},
  
  get courses(){
   return {
     appetizers: this._courses.appetizers,
     mains: this._courses.mains,
     desserts: this._courses.desserts,
   }
  },
  
  addDishToCourse (courseName, dishName, dishPrice){
    
    const dish = {
      _name: [],
      _price: [], 
      
      set name(dishName){
        if(typeof dishName === 'string'){
          this._name = dishName;
          console.log(`Great. ${dishName} has been added.`);
        }else{
          console.log(`${dishName} is not a valid choice.`);
        }
      },
      
      set price(dishPrice){
        if(typeof dishPrice === 'number'){
          this._price = dishPrice;
          console.log(`Great. ${dishPrice} has been added ...`);
        }else{
          console.log(`${dishPrice} is not a dollar amount.`);
        }
        
  		       
      },
      
  	}; /* closes dish dictionary */
    
    if (this._courses[courseName] == undefined){
      console.log('oops')
    } else {	
      dish.name = dishName
      dish.price = dishPrice
    	this._courses[courseName].push(dish);
    }
      },
  

  
  getRandomDishFromCourse(courseName) {
   
      const dishes = this._courses[courseName];
      const randomIndex = Math.floor(Math.random() * dishes.length);
    
  },
  
  generateRandomMeal() {
  const appetizer = this.getRandomDishFromCourse('appetizers');
  const main = this.getRandomDishFromCourse('mains');
  const dessert = this. getRandomDishFromCourse('desserts');
    
  const totalPrice = this.addDishToCourse('appetizers') + this.addDishToCourse('main') + this.addDishToCourse('desserts');
    return `Your meal is ${appetizer}, ${main}, and your dessert is ${dessert}. Your total is $${totalPrice}.`;
//Should I use ${totalPice}.tofixed here? If so, why?    
}
    };

menu.addDishToCourse('appetizers','Pot Stickers', 6.25);
menu.addDishToCourse('appetizers','Wanton Tacos', 7.25);

menu.addDishToCourse('mains','Baby Back Ribs', 21.25);
menu.addDishToCourse('mains ','Garlic Crusted Chicken', 12.50);

menu.addDishToCourse('desserts','Blondie', 8.30);
menu.addDishToCourse('desserts','Brownie Sundae', 9.15);

console.log(menu.courses)
let meal = menu.generateRandomMeal();

console.log(meal);

we define the dictionary (dish) but then never adding a name and a price to it, so the setter methods never do anything

we still have a lot of undefined, not sure where they are coming from, but at least some entries make it into our courses object


#18

Oh yeah that did a lot! That really helps! I also just realized that this in this case refers to the dish object. I was totally off.


#19

its a step in the right direction, but its certainly not done yet. You think this gives you enough leads to continue for now?


#20

Yes, it does. Thank you for all of the extra time you put in helping me out. This is a hard one.