A third error message in JS Meal Maker that I can't solve

The following error message is occurring for Meal Maker. My code is following the error message. (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):

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:37
this._courseName[courseName] };
^

TypeError: Cannot read property ‘appetizer’ of undefined
at Object.addDishToCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:37:23)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:65:6)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

const menu = {
  _courses : {
    appetizers : [],
    mains : [],
    dessert : [],
  },
    get appetizers() {
      return this._courses.appetizers;
    },
    get mains() {
      return this._courses.mains;
    },
    get desserts() {
      return this._courses.desserts;
    },
    set appetizers(appetizerIn) {
      this._courses.appetizers = appetizer;
    },
    set mains(mainIn) {
      this._courses.mains = main;
    },
    set desserts(dessertIn) {
      this.courses.desserts = dessert;
    },

    get courses() {
      return{
        appetizers: this.appetizers,
        mains: this.mains,
        desserts: this.desserts,
      }
    },

  addDishToCourse (courseName, dishName, dishPrice) {
      (!(courseName in this._courses)); 
    {
      this._courseName[courseName] }; 
    
    const dish = {
      courseName : [],
      name: dishName,
      price: dishPrice,
    }     
    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}, ${dessert.name}. The price is $${totalPrice}.`;
  },
  };


menu.addDishToCourse('appetizer', 'salad', 4.00);
menu.addDishToCourse('appetizer', 'duck', 4.50);
menu.addDishToCourse('appetizer', 'soup', 3.50);

menu.addDishToCourse('main', 'steak', 24.00);
menu.addDishToCourse('main', 'fish', 16.00);
menu.addDishToCourse('main', 'pork', 20.00);

menu.addDishToCourse('dessert', 'pie', 7.00);
menu.addDishToCourse('dessert', 'icecream', 6.00);
menu.addDishToCourse('dessert', 'cheesecake', 7.00);

 const meal = 
 menu.generateRandomMeal()

console.log(meal);





Hi Vickyrai,
Your menu.addDishToCourse() invocations aren’t correct; the first argument should be the name of a course (for example, ‘appetizers’ rather than ‘appetizer’). Check your _courses object as well; you’re going to run into a problem when you try to get a dessert.

1 Like

Hi @tera0976883653,

Thank you. I changed those things! Now its saying:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:37
this._courseName[courseName] };
^

TypeError: Cannot read property ‘appetizers’ of undefined
at Object.addDishToCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:37:23)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:65:6)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

Hi Vickyrai,
The way I’ve got it to work is here: https://jsfiddle.net/nvp6f9xk/12/

But if you still want to get it to work yourself without seeing the solution, really make sure that every time you’re accessing a course that it’s a plural (for example, if you were to try to get a random appetizer, you’d be getting one of the ‘appetizers’, not one of the ‘appetizer’).

One thing you can do that’ll really help with debugging is to console.log the thing that’s causing a problem. For example, you can see that there’s a problem with courseName at line 37; try logging the courseName to the console before it’s used in a function, which will allow you to check that the right thing is being input.

1 Like

Thank you so much for your reply! Was it the following snippet of the code that you were referring to:


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}, ${dessert.name}. The price is $${totalPrice}.`;
  },
  };

Should the created variables i.e const appetizer, be plurals?

Sorry, I was trying to be a bit oblique so that I didn’t give the answer away, but probably just ended up being unclear.

The variables are correct, the this.getRandomDishFromCourse() invocations aren’t. The courses are called ‘appetizers’, ‘mains’ and ‘desserts’.

1 Like

Thanks again! So, I’ve changed those to plurals, and it’s still saying:

objects-meal-maker/app.js:37
this._courseName[courseName] };
^

TypeError: Cannot read property ‘appetizers’ of undefined

Get rid of this bit in addDishToCourse:

(!(courseName in this._courses)); 
    {
      this._courseName[courseName] };

If it’s still broken, I’d recommend having a quick look at the jsfiddle link that I posted and comparing line by line.

1 Like

@tera0976883653 It’s working!! Thank you!! But for some reason the price isn’t displaying the ‘0’ in, for example, $6.50 .

Awesome, congrats! This one is a bit of a pain but it’s a good learning process, I think, partly in terms of really spell-checking your code (although VSC actually does a lot of that for you, which is nice).

The missing 0 is because JS isn’t like MS Excel - if you put in a number, it won’t convert it to a price, so 4.5, 4.50 and 4.500000000 are all given as 4.5 (because they are). If you really want it to show the 0 cents, you’ll have to convert the prices into strings (or try doing an if thing, but that would be a massive pain).

You could also use the method shown here, where you woud save the formatted numbers as variables and then refer to them instead.

1 Like

Because I was curious, I had a go:

formatter(number){
  let money = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD',
  minimumFractionDigits: 2
	});
  return money.format(number);
  },

    
generateRandomMeal() {
    const appetizer 
    = this.getRandomDishFromCourse('appetizers');
    const main 
    = this.getRandomDishFromCourse('mains');
    const dessert 
    = this.getRandomDishFromCourse('desserts');
    const totalPrice = appetizer.price + main.price + dessert.price;
    let formattedPrice = this.formatter(totalPrice);
    return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name}. The price is $${formattedPrice}.`;
  },

which worked for me…!

1 Like

@tera0976883653 Well done! Converting into strings doesn’t work because the following prints:

Your meal is salad, pork, cheesecake. The price is $4.0020.007.00.

Yeah, I tried that and was also surprised by that…! You’d have to just convert the last price into a string, but you’ll have already lost the decimals by that point. You could probably write a function that does the following:

If number is an integer, convert to string and add .00;
else if number ends in a single decimal, convert to string and add a 0 on the end;
return modified number.

But that’s a bit of a hack and only works if you’re dealing with pretty round numbers.

1 Like

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