Meal Maker Fun and Sadness. Please help


#1

Hello everyone,

I’ve been having a bit of trouble with this project, but I’m slowly getting it together. A couple of questions however. I’m having a hard time getting the name of the meals to appear when calling menu.generateRandomMeal(); I get the results below:

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

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

Here is my code, I know I’m overlooking something. I would really appreciate any help and feedback.



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.`);
        }
        
  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('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);

 


Thank you all in advance. Everytime I click on ‘save’ and get this result, all I hear is ‘Hello darkness my old friend…’.


Build a Library
#2

I’d add prints in the places that should have been handling that data to figure out where it got lost.


#3

Hey Ionatan,

I’m back on today and we fixed some of this in a separate thread. We were able to get it to print out the desserts and the appetizers , but the mains are still undefined. Here’s the updated code:

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

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('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); 

This results in this output:

Great. Pot Stickers has been added.
Great. 6.25 has been added ...
Great. Wanton Tacos has been added.
Great. 7.25 has been added ...
oops
oops
Great. Blondie has been added.
Great. 8.3 has been added ...
Great. Brownie Sundae has been added.
Great. 9.15 has been added ...
undefined is not a valid choice.
undefined is not a dollar amount.
oops
undefined is not a valid choice.
undefined is not a dollar amount.
Your meal is undefined, undefined, and your dessert is undefined. Your total is $NaN.```

#4

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

You defined the course as mains not main


#5

Ah, I see. I completely missed that. After fixing that the output is :

Great. Pot Stickers has been added.
Great. 6.25 has been added ...
Great. Wanton Tacos has been added.
Great. 7.25 has been added ...
Great. Baby Back Ribs has been added.
Great. 21.25 has been added ...
Great. Garlic Crusted Chicken has been added.
Great. 12.5 has been added ...
Great. Blondie has been added.
Great. 8.3 has been added ...
Great. Brownie Sundae has been added.
Great. 9.15 has been added ...
undefined is not a valid choice.
undefined is not a dollar amount.
oops
undefined is not a valid choice.
undefined is not a dollar amount.
Your meal is undefined, undefined, and your dessert is undefined. Your total is $NaN.

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.