Using .length() on a variable inside of an object

I am trying to use .length() on a variable but the variable is inside of an object and JavaScript considers .length() to be a key. How can I get around this?

I am on this lesson.

  addDishToCourse (courseName, dishName, dishPrice) { 
     const dish = {

     food: courseName,
     name: dishName,
     price: [],
     totalPrice: 0,
   };   
     
    for (var i = 0; i < this.price.length; i++) { // The problem is "this.price.length" specifically ".length"
         this.totalPrice = this.totalPrice + this.price[i];
      }

  },

I am not hugely au fait with JavaScript, but I’ve seen a fair few learners asking about the meal maker project… but I don’t quite understand your approach here.

Why would you need an array to store price, and why would there be a cumulative price - implied by totalPrice for a single dish on the menu?

Also, I don’t think that this refers to what you think it does… in that, you seem to be trying to use this to access the totalPrice property of your dish object… but - and this is an assumption, based on what I know of the project as I can’t refer to your entire program - at that point this = your menu object, not the dish which has the totalPrice property…

2 Likes

Price is cumulative because each part of the meal cost something. In this you have a main course with a dessert and appetizer, these all cost different things. I am adding all of these together so that when I log the final meal I have the total price instead of the price of just one item that they ordered. Also, thank you for telling me about the “this” thing.

My full code is:

var 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.desserts = desserts;
  },
set mains(mains) {
   this.mains = mains;
},
set desserts(desserts) {
     this.desserts = desserts;
  },
  get courses() {
    return {
      appetizers,
      mains,
      desserts,
    };
  },
  addDishToCourse (courseName, dishName, dishPrice) { 
     const dish = {

     food: courseName,
     name: dishName,
     price: [],
     totalPrice: 0,
   };   
     
    for (var i = 0; i < 8; i++) {
         this.totalPrice = this.totalPrice + this.price[i];
      }

  },
     getRandomDishFromCourse: function(courseName) {
    var dishes = this._courses[courseName];
     if (dishes !== undefined) {
       var randomIndex = Math.floor(Math.random() * dishes.length);
      return randomIndex;
     }
  },
  
  generateRandomMeal: function() { //This is the problem.
   var appetizer = this.getRandomDishFromCourse('appetizers');
    var main = this.getRandomDishFromCourse('mains');
    var dessert = this.getRandomDishFromCourse('dessert');
    return 'Your meal is ' + this.appetizers[appetizer].name + ', ' + this.mains[main].name + ', and ' + this.desserts[dessert] + '. The total price is $' + this.totalPrice   + '.'; 
  }
}; 

menu.addDishToCourse('appetizers', 'Bread Loaf', 4.25);
menu.addDishToCourse('appetizers', 'Caesar Salad', 3.50);
menu.addDishToCourse('appetizers', 'Small Soup', 4.50);
menu.addDishToCourse('mains', 'Steak Dinner', 20.00);
menu.addDishToCourse('mains', 'Spaghetti', 16.00);
menu.addDishToCourse('mains', 'Turkey Dinner', 18.99);
menu.addDishToCourse('desserts', 'Icecream', 5.99);
menu.addDishToCourse('desserts', 'Cookies', 5.99);
menu.addDishToCourse('desserts', 'Jello', 5.99);


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

I would suggest you follow the steps more closely if you’re still running into issues with this project.
Step 8 tells you how your dish object should be setup.

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

Your dish object should only have two keys, name and price, after you create the dish object you .push() it into the correct array inside the _courses object based on the courseName

Should I just try putting this code somewhere and restarting?

I would do that if I was in your situation. Just don’t completely delete the code because you do have somethings correct but you’re going off track in some parts. Based on your code right now I would leave everything above addDishToCourse. Just make sure you’re following the steps exactly and test/debug every step as well. Like when you write addDishToCourse again, console.log() the array of the courseName you sent as a parameter. Are the dishes you added showing up in the array? How do you access the price of the dish object inside of that array? If you still have any questions about specific steps we’re here to help.

1 Like

Yes, that makes perfect sense - what’s confusing is why you would have this saved as a property of a dish object, which is a single component part of the greater meal.

If you mean "restart on addDishToCourse", then sure. If you mean start completely from scratch, you don’t need to - the rest of your program looks fine at first glance.

I think you’re trying to do a couple of different things at once in this function, whereas the function that the project is trying to get you to write does only one thing.

1 Like