Javascript Meal Maker Price Total Help

Welcome to the Get Help category!

This is where you can ask questions about your code. Some important things to remember when posting in this category :slight_smile:

  • Learn how to ask a good question and get a good answer!
  • Remember to include a link to the exercise you need help with!
  • If someone answers your question, please mark their response as a solution :white_check_mark:
  • Once you understand a new concept, come back and try to help someone else!

Hi all, when i run my code my “Total Price” is sometimes outputting a strange decimal and I’m just wondering why!?

here’s an example of what is being outputted at times,

“Your 3 course meal will start with Fresh Season Vegetables, followed by Fish & Chips for your main course and Chocolate Lava Cake for dessert! Your bill for this evening will be 16.990000000000002.”

#########################################
Here is my code for the project:

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: []
  },
  get courses() {
    return {
      appetizers: this.appetisers,
      mains: this.mains,
      desserts: this.desserts
    }
  },

  // ALLOWS YOU TO ADD A DISH OBJECT TO EACH COURSE ARRAY
  addDishToCourse(courseName, dishName, dishPrice) {
    let dish = {
      name: dishName,
      price: dishPrice
    }

    this._courses[courseName].push(dish);
  },

  // GETS RANDOM DISH FROM THE COURSE THAT IS PASSED IN
  getRandomDishFromCourse(courseName) {
    let dishes = this[courseName];
    let randomIndex = Math.floor(Math.random() * dishes.length);
    return dishes[randomIndex];
  },

  // GENERATES A 3 COURSE MEAL AND RETURNS IT AS A STRING WITH THE TOTAL BILL
  generateRandomMeal() {
    const appetizer = this.getRandomDishFromCourse('appetizers');
    const main = this.getRandomDishFromCourse('mains');
    const dessert = this.getRandomDishFromCourse('desserts');
    const billTotal = appetizer.price + main.price + dessert.price;
    return `Your 3 course meal will start with ${appetizer.name}, followed by ${main.name} for your main course and ${dessert.name} for dessert! Your bill for this evening will be ${billTotal}.`
  },

  // GETTERS & SETTERS
  get appetizers() {
    return this._courses.appetizers;
  },
  set appetizers(appetizerIndex) {

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

  },
  get desserts() {
    return this._courses.desserts;
  },
  set desserts(dessertIndex) {

  }
}


// Add Appetizers
menu.addDishToCourse('appetizers', 'Mushroom Soup', 3.50);
menu.addDishToCourse('appetizers', 'Baked Spinach & Cheese', 5.50);
menu.addDishToCourse('appetizers', 'Fresh Season Vegetables', 3.50);

// Add Mains
menu.addDishToCourse('mains', 'Roasted Duck', 8.99);
menu.addDishToCourse('mains', 'Fish & Chips', 8.50);
menu.addDishToCourse('mains', 'Rib Eye Steak', 11.99);

// Add Deserts
menu.addDishToCourse('desserts', 'Chocolate Lava Cake', 4.99);
menu.addDishToCourse('desserts', 'Tiramisu', 3.99);
menu.addDishToCourse('desserts', 'Apple Pie', 3.50);



// TRY IT OUT, GENERATE A 3 COURSE MEAL
const meal = menu.generateRandomMeal();

console.log(meal);

###################################################

Hope someone can clear it up as I’m sure its pretty simple but i can’t seem to figure out why :sweat_smile:

Thank You,
Julian
:v:

I would read this stackoverflow answer:

How to deal with floating point number precision in JavaScript? - Stack Overflow

which summarizes the problem and suggest a couple solution based on what you need

Perfect, thanks! :smiley::v: