Menu Maker Project

Trying to work on the menu maker project in the learn javascript program.

Got through the beginning sections no problem, but running into an error when I get to the generateRandomMeal function.

If someone could please help me figure out what I’ve done wrong here, I’d be greatly appreciative.

I’m getting a syntax error in the above mentioned function, at the colon on the ‘main’ variable.

My code is included below

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

Hi there, Just from a quick look I can see you’re missing a comma (,) on generateRandomMeal():

generateRandomMeal() {
  appetizer: this.getRandomDishFromCourse(appetizers),
  main: this.getRandomDishFromCourse(mains) // Right here!
  dessert: this.getRandomDishFromCourse(desserts),
  total: appetizer[2] + main[2] + dessert[2],
}

Maybe that will fix it?

Dear @ryanchipeniuk5549113
there are several problems with the function generateRandomMeal().

  1. It is a function, not an object: you cannot use colons for setting values of variables, instead you have to use the equal sign. And you would need to end each line with a semicolon instead of a comma.
  2. All variables inside the scope are not declared. appetizer, main, dessert and total are undeclared.
  3. You do not return anything from that function. When you call the function, it does not have anything to print to the console.
  4. You have to pass the arguments within the getRandomDishFromCourse function call as strings.

What is the function supposed to do exactly? (You should also check the values of your _course object: With the addDishToCourse function, you cannot add valid dishes.)

1 Like

it’s become quite clear to me as i continue to debug that there are many more problems than the above-mentioned. I don’t know why I’m having so much trouble with this particular project, as it’s the first one that I’ve needed more than 1 or 2 debug passes through, and i’ve completed stuff that came up after it without any issue.

I’ll update the code above after a few more passes

I think everyone has trouble with this one - it took me ages and even then I had to watch the walkthrough video to find what I did wrong when I did it. I think it’s the first project you do where a tiny little mistake (like using the syntax for an object rather than for a function, or forgetting when you should use ‘appetizer’ and when you should use ‘appetizers’) will break the entire code in a way that’s really difficult to debug. Don’t stress and keep plugging away, you’ll get there - and there’s no shame in watching the walkthrough to see the ‘right’ way to do it, comparing your version to the ‘official’ version and then working out why your version didn’t work. It’s all part of the learning process. :fist_right:

1 Like

Another question from my side…

One thing I can’t get my head around is in step 8:

// step 8.1 The .addDishToCourse() method should create an object called dish which has a name and price which it gets from the parameters.

// Step 8.2 The method should then push this dish object into the appropriate array in your menu ‘s _courses object based on what courseName was passed in.

  addDishToCourse(courseName, dishName, dishPrice) {

    const dish = {

      name: dishName,

      price: dishPrice

    };

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

  },

How come we use [courseName] here? I get that appetizers, mains, and desserts all contain arrays, but how come these are being treated as arrays as well? Are we essentially pushing arrays into arrays? I feel like I missed something.

Here’s the code I am referring to:

_courses: {

    appetizers: [],

    mains: [],

    desserts: []

  },

  // getters

  get appetizers() {

    return this._courses.appetizers;

  },

  get mains() {

    return this._courses.mains;

  },

  get desserts() {

    return this._courses.desserts;

  },

  // setters

  set appetizers(appetizers) {

    this._courses.appetizers = appetizers;

  },

  set mains(mains) {

  this._courses.mains = mains;

  },

  set desserts(desserts) {

  this._courses.desserts = desserts;

  },

Hi @rjorr04
the function addDishToCourse actually adds an object with two key-value pairs to an array inside the _courses object. By passing the courseName as an argument, your function gets the information, to which array the dish should be pushed to: either appetizers, mains or desserts.
If you log the appetizers inside your _courses object, it should look like this:

console.log(menu._courses.appetizers);

[{
  dishName: "tomato soup",
  dishPrice: 8.5
}, {
  dishName: "ceasar salad",
  dishPrice: 6.5
}]
1 Like

My code’s gone through a few swap arounds, so I thought I’d repost it again for more help.

Ironed out most of my just syntax based errors, but still having runtime issues that seem to be centered around the get _courses portion of the code re: line 25ish

const menu = {
    _courses: {
        appetizers: [],
        get appetizers() {
            return this._appetizers;
        },
        set appetizers(appetizer) {
            this._appetizers = appetizer;
        },
        mains: [],
        get mains() {
            return this.mains;
        },
        set mains(value) {
            this.mains = value;
        },
        desserts: [],
        get desserts() {
            return this.desserts;
        },
        set desserts(dessert) {
            this.desserts = dessert;
        },
    },
	get _courses() {
		return {
			appetizers: this._courses.appetizers,
			mains: this._courses.mains,
			desserts: this._courses.desserts
			};
		},
	addDishToCourse(courseName, dishName = '', dishPrice = 0) {
		const dish = {
			courseName,
			dishName,
			dishPrice
		};
		this._courses[courseName].push(dish);
	},
	getRandomDishFromCourse(courseName) {
		const dish = this._courses[courseName];
		const randomIndex = Math.floor(Math.random(0 * dish.length));
		return dish[randomIndex];
	},
	generateRandomMeal() {
		const appetizer = this.getRandomDishFromCourse(appetizers);
		const main = this.getRandomDishFromCourse(mains);
		const dessert = this.getRandomDishFromCourse(desserts);
		const total = appetizer[2] + main[2] + dessert[2];
		console.log(`Appetizer: ${appetizer[1]}, Main Course: ${main[1]}, Dessert: ${dessert[1]}. Your total today will be ${total}.`);
	}
};
	menu.addDishToCourse('appetizers', 'Mozza Sticks', 5);
	menu.addDishToCourse('appetizers', 'Poutine', 10);
	menu.addDishToCourse('appetizers', 'Wings', 15);
	menu.addDishToCourse('mains', 'Chicken', 10);
	menu.addDishToCourse('mains', 'Steak', 15);
	menu.addDishToCourse('mains', 'Lobster', 20);
	menu.addDishToCourse('desserts', 'Tiramisu', 5);
	menu.addDishToCourse('desserts', 'Sundae', 7.50);
	menu.addDishToCourse('desserts', 'Chocolate cake', 9);
	
	generateRandomMeal();
	generateRandomMeal();
	generateRandomMeal();

i tried a more generic getter method, and printed a lot of console.log lines from different points in the code, and i’m pretty sure it’s a reference error there. Do I need to move the getter method up in the code? is it nested incorrectly? only ideas i have at the moment.

Hi @ryanchipeniuk5549113
from a quick review I found several problems:

  1. You should remove your getters and setters from the _courses object, you do have duplicate keys there now. You should move them below the _courses object.
  2. The AddDishToCourse method still can’t work properly: Your price is always 0 and your dishname always an empty string, because you set the values of the arguments to fix values inside the method definition. You also shouldn’t print the courseName to the dish, the dish should be an object with two key-value pairs (dishName and dishPrice). Please see my post above: It shows what the value of appetizers inside the _courses object should look like.
  3. Your getRandomDishFromCourse method can’t return anything valid, because Math.random() does not take any arguments.
3 Likes