|
1 | 1 | let john = { |
2 | | - bills : [124,48,268,180,42], |
3 | | - allTips : [], |
4 | | - finalAmounts : [], |
5 | | - calcTips : function(){ |
6 | | - for(let i=0;i<this.bills.length;i++){ |
7 | | - if(this.bills[i]<50){ |
8 | | - this.allTips.push(this.bills[i]*0.2); |
9 | | - this.finalAmounts.push(this.allTips[i]+this.bills[i]); |
10 | | - } |
11 | | - else if(this.bills[i]<200){ |
12 | | - this.allTips.push(this.bills[i]*0.15); |
13 | | - this.finalAmounts.push(this.allTips[i]+this.bills[i]); |
| 2 | + bills: [124,48,268,180,42], |
| 3 | + allTips: [], |
| 4 | + finalAmounts: [], |
| 5 | + |
| 6 | + calcTips() { |
| 7 | + this.bills.forEach(bill => { |
| 8 | + |
| 9 | + let tip; |
| 10 | + |
| 11 | + if (bill < 50){ |
| 12 | + tip = bill * 0.2; |
| 13 | + this.allTips.push(tip); |
| 14 | + this.finalAmounts.push(tip + bill); |
14 | 15 | } |
15 | | - else{ |
16 | | - this.allTips.push(this.bills[i]*0.1); |
17 | | - this.finalAmounts.push(this.allTips[i]+this.bills[i]); |
| 16 | + else if (bill < 200) { |
| 17 | + tip = bill * 0.15; |
| 18 | + this.allTips.push(tip); |
| 19 | + this.finalAmounts.push(tip + bill); |
18 | 20 | } |
19 | | - } |
| 21 | + else { |
| 22 | + tip = bill * 0.1; |
| 23 | + this.allTips.push(tip); |
| 24 | + this.finalAmounts.push(tip + bill); |
| 25 | + } |
| 26 | + }); |
20 | 27 | } |
21 | 28 | } |
| 29 | + |
22 | 30 | john.calcTips(); |
| 31 | + |
23 | 32 | console.log(john.allTips); |
24 | 33 | console.log(john.finalAmounts); |
25 | 34 |
|
26 | 35 | //Extra |
27 | 36 |
|
28 | 37 | let mark = { |
29 | | - bills : [77,375,110,45], |
30 | | - allTips : [], |
31 | | - finalAmounts : [], |
32 | | - calcTips : function(){ |
33 | | - for(let i=0;i<this.bills.length;i++){ |
34 | | - if(this.bills[i]<100){ |
35 | | - this.allTips.push(this.bills[i]*0.2); |
36 | | - this.finalAmounts.push(this.allTips[i]+this.bills[i]); |
| 38 | + bills: [77,375,110,45], |
| 39 | + allTips: [], |
| 40 | + finalAmounts: [], |
| 41 | + |
| 42 | + calcTips() { |
| 43 | + this.bills.forEach(bill => { |
| 44 | + let tip; |
| 45 | + if (bill < 100) { |
| 46 | + tip = bill * 0.2; |
| 47 | + this.allTips.push(tip); |
| 48 | + this.finalAmounts.push(tip + bill); |
37 | 49 | } |
38 | | - else if(this.bills[i]<300){ |
39 | | - this.allTips.push(this.bills[i]*0.1); |
40 | | - this.finalAmounts.push(this.allTips[i]+this.bills[i]); |
| 50 | + else if (bill < 300) { |
| 51 | + tip = bill * 0.1; |
| 52 | + this.allTips.push(tip); |
| 53 | + this.finalAmounts.push(tip + bill); |
41 | 54 | } |
42 | | - else{ |
43 | | - this.allTips.push(this.bills[i]*0.25); |
44 | | - this.finalAmounts.push(this.allTips[i]+this.bills[i]); |
| 55 | + else { |
| 56 | + tip = bill * 0.25; |
| 57 | + this.allTips.push(tip); |
| 58 | + this.finalAmounts.push(tip + bill); |
45 | 59 | } |
46 | | - } |
| 60 | + }); |
47 | 61 | } |
48 | 62 | } |
49 | 63 |
|
50 | | -function tipsAvrg(tips){ |
51 | | - let sum=0; |
52 | | - for(let i=0;i<tips.length;i++){ |
53 | | - sum+=tips[i]; |
54 | | - } |
| 64 | +function tipsAvrg(tips) { |
| 65 | + |
| 66 | + let sum = 0; |
| 67 | + |
| 68 | + tips.forEach(tip => { |
| 69 | + |
| 70 | + sum += tip; |
| 71 | + }); |
| 72 | + |
55 | 73 | return sum/tips.length; |
56 | 74 | } |
57 | 75 |
|
58 | 76 | mark.calcTips(); |
| 77 | + |
59 | 78 | john['avrgTips'] = tipsAvrg(john.allTips); |
60 | 79 | mark['avrgTips'] = tipsAvrg(mark.allTips); |
61 | 80 |
|
62 | | -let highestTips='John and Mark have paid the same amount of average tips'; |
| 81 | +let highestTips = 'John and Mark have paid the same amount of average tips'; |
| 82 | + |
| 83 | +if (john.avrgTips !== mark.avrgTips) { |
63 | 84 |
|
64 | | -if(john.avrgTips !== mark.avrgTips){ |
65 | | - highestTips = john.avrgTips>mark.tipsAvrg?"John's family has paid the highest amount of tips":"Mark's family has paid the highest amount of tips"; |
| 85 | + highestTips = john.avrgTips > mark.tipsAvrg ? "John's family has paid the highest amount of tips" : "Mark's family has paid the highest amount of tips"; |
66 | 86 | } |
| 87 | + |
67 | 88 | console.log(highestTips); |
0 commit comments