Issue with MEAL MAKER - PRICE OUTPUT- DECIMAL IS WAY TOO LONG

There must be a way to lower the decimal of the price down to the hundredth without changing the actual price. I tried several different Math methods, but I can’t seem to wrap around how to fix the issue without changing the actual prices to lower amounts.

const menu = { _courses: { appetizers: [], mains: [], deserts: [] }, get appetizers() { return this._courses.appetizers; }, get mains() { return this._courses.mains; }, get deserts() { return this._courses.deserts; }, set appetizers(appetizers) { return this._courses.appetizers = appetizers; }, set mains(mains) { return this._courses.mains = mains; }, set deserts(deserts) { return this._courses.deserts = deserts; }, get courses() { // return the getter methods, you could also just return _courses as it is. return { appetizers: this.appetizers, mains: this.mains, deserts: this.deserts }; }, addDishToCourse(courseName, dishName, dishPrice) { const dish = { name: dishName, price: dishPrice, }; return this._courses[courseName].push(dish); }, getRandomDishFromCourse(courseName) { // step 9-10: getting this method to work const dishes = this._courses[courseName]; // retrieve the array of the given course's dishes from the menu's _courses object and store in a variable. const randomIndex = Math.floor(Math.random() * dishes.length);// generate random index by length of the dishes array. (This will guarantee that the random num will be between 0 and the length of the array.) return dishes[randomIndex]; // return the dish located at that index in 'dishes'. }, generateRandomMeal() { // step 11 // - Create an appetizer variable which should be the result of calling the .getRandomDishFromCourse() method when // we pass in an appetizers string to it. const appetizer = this.getRandomDishFromCourse('appetizers'); // - Create a main variable and dessert variable the same way you created the appetizer variable, // but make sure to pass in the right course type. const desert = this.getRandomDishFromCourse('deserts'); const main = this.getRandomDishFromCourse('mains'); // - Calculates the total price and returns a string that contains the name of each of the dishes and the // total price of the meal, formatted as you like. const totalPrice = appetizer.price + main.price + desert.price; return `Your meal is ${appetizer.name}, ${main.name}, ${desert.name}...The price is $${totalPrice}.`; } }; menu.addDishToCourse('appetizers', 'Michigan Salad', 12.00); menu.addDishToCourse('appetizers', 'Shuba', 35.00); menu.addDishToCourse('appetizers', 'Pickled Tomatoes', 8.00); menu.addDishToCourse('mains', 'Ox Leg', 875.95); menu.addDishToCourse('mains', 'Lamb Tongue', 546.95); menu.addDishToCourse('mains', 'Goat Ribs', 777.95); menu.addDishToCourse('deserts', 'Napoleon', 95.98); menu.addDishToCourse('deserts', 'Coconut Cream Cake', 75.98); menu.addDishToCourse('deserts', 'Lebanese Rice Pudding', 65.98); const meal = menu.generateRandomMeal(); console.log(meal);

Hello @leonlemeshko88474957, welcome to the forums! That’s what’s called a “float rounding error,” and happens (only sometimes) because computers have to convert base 10 into binary, and back again; it’s like trying to turn 1/3 into decimal-you get 0.3333r, and since floats have 15 decimal places, the output will contain that many. Here’s a good article on the topic.


One solution would be to just round when embedding your number variable into a string. This StackOverflow thread is very helpful in that regard.

Maybe the string formatting stuff (mentioned in the previous post) would help.
I think .toFixed(2) returns the a string version of the number rounded/written to 2 decimal places.
One possibility:

`The price is $${totalPrice.toFixed(2)}`   

Here’s a demonstration:

var totalPrice = 15.8175; var str = `The price is $${totalPrice.toFixed(2)}`; console.log(str);

I am so grateful to both replies, I will try both trouble shoots later today and let you know. Thank you so much for being so responsive on here

1 Like