Skip to content

Commit 5633149

Browse files
committed
Fixed the code's format
1 parent f8a9554 commit 5633149

1 file changed

Lines changed: 60 additions & 39 deletions

File tree

‎script.js‎

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,88 @@
11
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);
1415
}
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);
1820
}
19-
}
21+
else {
22+
tip = bill * 0.1;
23+
this.allTips.push(tip);
24+
this.finalAmounts.push(tip + bill);
25+
}
26+
});
2027
}
2128
}
29+
2230
john.calcTips();
31+
2332
console.log(john.allTips);
2433
console.log(john.finalAmounts);
2534

2635
//Extra
2736

2837
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);
3749
}
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);
4154
}
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);
4559
}
46-
}
60+
});
4761
}
4862
}
4963

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+
5573
return sum/tips.length;
5674
}
5775

5876
mark.calcTips();
77+
5978
john['avrgTips'] = tipsAvrg(john.allTips);
6079
mark['avrgTips'] = tipsAvrg(mark.allTips);
6180

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) {
6384

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";
6686
}
87+
6788
console.log(highestTips);

0 commit comments

Comments
 (0)