-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge5.js
More file actions
69 lines (60 loc) · 1.69 KB
/
challenge5.js
File metadata and controls
69 lines (60 loc) · 1.69 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var john = {name: 'John',
bills: [124, 48, 268, 180, 42],
calcTips: function() {
this.tips = [];
this.totals = [];
for (var i = 0; i < this.bills.length; i++) {
if (this.bills[i] < 50) {
this.tips.push(this.bills[i] * 0.2);
this.totals.push(this.bills[i] + (this.bills[i] * 0.2));
} else if (this.bills[i] >= 50 && this.bills[i] < 200) {
this.tips.push(this.bills[i] * 0.15);
this.totals.push(this.bills[i] + (this.bills[i] * 0.15));
} else {
this.tips.push(this.bills[i] * 0.1);
this.totals.push(this.bills[i] + (this.bills[i] * 0.1));
}
}
}
};
var mark = {name: 'Mark',
bills: [77, 375, 110, 45],
calcTips: function() {
this.tips = [];
this.totals = [];
for (var i = 0; i < this.bills.length; i++) {
if (this.bills[i] < 100) {
this.tips.push(this.bills[i] * 0.2);
this.totals.push(this.bills[i] + (this.bills[i] * 0.2));
} else if (this.bills[i] >= 100 && this.bills[i] < 300) {
this.tips.push(this.bills[i] * 0.1);
this.totals.push(this.bills[i] + (this.bills[i] * 0.1));
} else {
this.tips.push(this.bills[i] * 0.25);
this.totals.push(this.bills[i] + (this.bills[i] * 0.25));
}
}
}
};
function getAverage(tips) {
tipSum = 0
for (var i = 0; i < tips.length; i++) {
tipSum += tips[i]
}
return tipSum/tips.length
};
john.calcTips();
mark.calcTips();
console.log(john.tips);
console.log(john.totals);
console.log(mark.tips);
console.log(mark.totals);
var johnAvg = getAverage(john.tips);
var markAvg = getAverage(mark.tips);
if (johnAvg == markAvg) {
console.log("They both tipped the same.");
} else if (johnAvg > markAvg) {
console.log("John's family tipped more.");
} else {
console.log("Mark's family tipped more.");
}