-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeal_calculator.js
More file actions
55 lines (45 loc) · 1.57 KB
/
Copy pathmeal_calculator.js
File metadata and controls
55 lines (45 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
To Do:
Create diner objects which represent a single diner.
Add dishes to a diner's meal
Total up the cost of all of the diners' meals
Add a fixed tax percentage to the total bill
Add a percentage tip to the total bill
Split the bill fairly among the diners
Each diner should pay the tax on their own food
Each diner should pay an equal share of the tip
If you choose to round the amounts, you may notice that the sum of the amounts does not equal the total bill amount anymore. Don't worry about that, or distribute the discrepancy in a fair way for an extra challenge.
Print out a total bill
Print a breakdown for what each diner owes
*/
var Diner = function(tip) {
console.log('Create diner object');
};
Diner.prototype = {
fixedTax: 0.06,
tip: 0.20,
meals: [],
total: 0,
addMeal: function(mealName, mealValue) {
this.meals.push({name: mealName, value: mealValue});
},
calcTotal: function() {
var total = 0;
this.meals.forEach(function(obj) {
total += obj.value;
});
total = this.addTax(total);
total = this.addTip(total);
this.total = total;
},
round: function(number) { return Math.round(number*100)/100; },
addTax: function(total) { return total*(this.fixedTax+1); },
addTip: function(total) { return this.round(total*(this.tip+1)) },
splitBill: function() { return this.round(this.total/this.meals.length); }
};
var instance = new Diner(0.15);
instance.addMeal('steak Ranchero', 8.50);
instance.addMeal('Fajitas', 10.50);
instance.calcTotal();
console.log('total: $', instance.total);
console.log('total per meal: $', instance.splitBill());