JS Chapter 8 (Objects) - Meal Maker project (Codecademy Pro)


#1

Hi , my solution works most of the time, that is, the generateRandomMeal function generates output like:

Your choice:

prawn coctail
sish kebap
vanilla ice cream

Total price: 13

But every now and then, I get the following error, and I’d like to know the cause, so that I can eliminate it.

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:61
let appName = this.app.name;
^

TypeError: Cannot read property ‘name’ of undefined
at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:61:27)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:87: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)

Link to exercise: https://www.codecademy.com/courses/introduction-to-javascript/projects/meal-maker

Here is my code:

let menu = {
_courses: {
apps: [],
mains: [],
dsts: []
},

get apps() {
return this._courses.apps;
} ,

set apps(dishname) {
let vals = dishname.split(" ");
this.meal = vals[0];
this.dishprice = vals[1];
this.dish = {name: this.meal, price: this.dishprice};
this._courses.apps.push(this.dish);
console.log(this._courses.apps);
},

get mains() {
return this._courses.mains;
},

get dsts() {

return this._courses.dsts;
},

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

addDishToCourse(courseName, dishName, dishPrice) {
courseName,
dishName,
dishPrice,
this.dish = {
name: dishName,
price: dishPrice
};
this._courses[courseName].push(this.dish);
},

getRandomDishFromCourse(courseName) {
let dishes = this._courses[courseName];
// console.log(dishes.length);
let index = (Math.floor(Math.random(dishes.length -1) * 10 / dishes.length));
// console.log(index);
return dishes[index];
},
generateRandomMeal() {
this.app = this.getRandomDishFromCourse(“apps”);
this.mne = this.getRandomDishFromCourse(“mains”);
this.des = this.getRandomDishFromCourse(“dsts”);

let appName = this.app.name;
let mainName = this.mne.name;
let dessName = this.des.name;
let totPrice = this.app.price + this.mne.price + this.des.price;

console.log(`Your choice:

${appName}
${mainName}
${dessName}

Total price: ${totPrice}`);
}

}

menu.addDishToCourse(“mains”, “pizza”, 5);
menu.addDishToCourse(“mains”, “sish kebap”, 7);
menu.addDishToCourse(“mains”, “lamb vindaloo”, 11);
menu.addDishToCourse(“apps”, “prawn coctail”, 4);
menu.addDishToCourse(“apps”, “doku valoku”, 3.5);
menu.addDishToCourse(“apps”, “vegetable soup”, 5);
menu.addDishToCourse(“dsts”, “whisky soda”, 5);
menu.addDishToCourse(“dsts”, “vanilla ice cream”, 2);
menu.addDishToCourse(“dsts”, “stuffed chestnut”, 3);
// console.log(menu.getRandomDishFromCourse(“mains”));
menu.generateRandomMeal();


#2

Two things,

  1. Math.random() does not take any argument.

  2. Multiplying by a fixed literal means there must always be 10 items to select from…

    Math.random() * array.length

lets the array length be the determining factor. There can be any number of possible items to select from.


#3

Thanks a lot. That was certainly not the place where I would have looked.

Regards,
Frank.