'totalPrice' variable on line 77

Is not being used, it seems like it should reachable.

// Created by Paul A. Gureghian in May 2020. //
// This JavaScript program creates a three-course meal //
// Based upon what is available on a menu. //

// Start of program. //

// Create an object named "menu". //
const 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.appetizers = appetizers;
    },

    set "mains"(mains) {
        this._courses.mains = mains;
    },

    set "desserts"(desserts) {
        this._courses.desserts = desserts;
    },

    get "courses"() {

        return {

            "appetizers": this.appetizers,
            "mains": this.mains,
            "desserts": this.desserts
        
        };    
    },

    addDishToCourse(courseName, dishName, dishPrice) {

        const dish = {
            "name": dishName,
            "price": dishPrice, 
        };
    
        return 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('appetizers');
        const main = this.getRandomDishFromCourse('mains');
        const dessert = this.getRandomDishFromCourse('desserts');
        const totalPrice = appetizer.price + main.price + dessert.price;
        
        return "Your meal is ${appetizer.name}, ${main.name}, and ${dessert.name}, and the total price is ${totalPrice}";
    }
};

// Add dishes to courses. //
menu.addDishToCourse('appetizers', 'salad', 4.00);
menu.addDishToCourse('appetizers', 'wings', 4.50);
menu.addDishToCourse('appetiizers', 'fries', 5.00);

menu.addDishToCourse('mains', 'steak', 10.25);
menu.addDishToCourse('mains', 'salmon', 7.75);
menu.addDishToCourse('mains', 'pasta', 11.20);

menu.addDishToCourse('desserts', 'ice cream', 3.00);
menu.addDishToCourse('desserts', 'cake', 4.50);
menu.addDishToCourse('desserts', 'pie', 3.50);

// Call 'menu' object method 'generateRandomMeal()'. //
const meal = menu.generateRandomMeal();

console.log(meal);

// End of program. //



That isn’t the syntax for template strings, template strings use backticks. You’re just returning that string: there won’t be any interpolation, and because of that you aren’t using any of the variables.

Also:

  1. typo in one of the calls to add to menu at the end: appetiizers, this throws an error until fixed
  2. you don’t need to put the names of the methods in quote marks
  3. not quite sure why you’re using getters and setters here, this does the same thing (fair enough if you’re just practising getters and setters, this is possibly not best usecase though as it just makes things a little confusing):
function randomItem(arr) {
  const randomIndex = Math.floor(Math.random() * arr.length); 
  return arr[randomIndex]; 
}

const menu2 = {
  appetizers: [],
  mains: [],
  desserts: [],

  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      "name": dishName,
      "price": dishPrice, 
    };
    this[courseName].push(dish);
  },
    
  generateRandomMeal() {
    const appetizer = randomItem(this.appetizers);
    const main = randomItem(this.mains);
    const dessert = randomItem(this.desserts);
    const totalPrice = appetizer.price + main.price + dessert.price;
        
    return `Your meal is ${appetizer.name}, ${main.name}, and ${dessert.name}, and the total price is ${totalPrice}`;
  }
};

Thanks. all fixed now.

1 Like