Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b3f1edc

Browse files
src: Add solution to Coding Challenge 5
1 parent 8e4d0a1 commit b3f1edc

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>JavaScript Tutorials</title>
6+
</head>
7+
8+
<body>
9+
<h1>Coding Challenge 5</h1>
10+
</body>
11+
12+
<script src="index.js"></script>
13+
</html>
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
2+
// -----------------------------------------------------------------------------
3+
// Coding Challenge 5
4+
// -----------------------------------------------------------------------------
5+
6+
/*
7+
Remember the tip calculator challenge?
8+
Let's create a more advanced version using everything we learned!
9+
10+
This time, John and his family went to 5 different restaurants. The bills were
11+
124,ドル 48,ドル 268,ドル 180ドル and 42ドル. John likes to tip 20% of the bill when the bill
12+
is less than 50,ドル 15% when the bill is between 50ドル and 200,ドル and 10% if the bill
13+
is more than 200ドル.
14+
15+
Implement a tip calculator using objects and loops:
16+
1. Create an object with an array for the bill values
17+
2. Add a method to calculate the tip
18+
3. This method should include a loop to iterate over all the paid bills and
19+
do the tip calculations
20+
4. As an output, create 1) a new array containing all tips, and 2) an array
21+
containing final paid amounts (bill + tip). HINT: Start with two empty
22+
arrays [] as properties and then fill them up in the loop.
23+
24+
EXTRA AFTER FINISHING:
25+
Mark's family also went on a holiday, going to 4 different restaurants. The bills
26+
were 77,ドル 375,ドル 110,ドル and 45ドル. Mark likes to tip 20% of the bill when the bill is
27+
less than 100,ドル 10% when the bill is between 100ドル and 300,ドル and 25% if the bill is
28+
more than 300ドル (different than John).
29+
30+
5. Implement the same functionality as before, this time using Mark's tipping rules
31+
6. Create a function (not a method) to calculate the average of a given array of tips.
32+
HINT: Loop over the array, and in each iteration store the current sum in a
33+
variable (starting from 0). After you have the sum of the array, divide it by
34+
the number of elements in it (that's how you calculate the average)
35+
7. Calculate the average tip for each family
36+
8. Log to the console which family paid the highest tips on average
37+
38+
GOOD LUCK 😀
39+
*/
40+
41+
var john = {
42+
fullName: "John",
43+
bills: [124, 48, 268, 180, 42],
44+
tips: [],
45+
totals: [],
46+
calcOneTip: function(amount) {
47+
if (amount < 50) {
48+
return (20 / 100) * amount;
49+
} else if (amount >= 50 && amount <= 200) {
50+
return (15 / 100) * amount;
51+
} else {
52+
return (10 / 100) * amount;
53+
}
54+
},
55+
calcAllTips: function() {
56+
for (var i = 0; i < this.bills.length; i++) {
57+
this.tips.push(this.calcOneTip(this.bills[i]));
58+
}
59+
},
60+
calcTotals: function() {
61+
for (var i = 0; i < this.bills.length; i++) {
62+
this.totals.push(this.bills[i] + this.tips[i]);
63+
}
64+
}
65+
}
66+
john.calcAllTips();
67+
john.calcTotals();
68+
console.dir(john);
69+
70+
var mark = {
71+
fullName: "Mark",
72+
bills: [77, 375, 110, 45],
73+
tips: [],
74+
totals: [],
75+
calcOneTip: function(amount) {
76+
if (amount < 100) {
77+
return (20 / 100) * amount;
78+
} else if (amount >= 100 && amount <= 300) {
79+
return (10 / 100) * amount;
80+
} else {
81+
return (25 / 100) * amount;
82+
}
83+
},
84+
calcAllTips: function() {
85+
for (var i = 0; i < this.bills.length; i++) {
86+
this.tips.push(this.calcOneTip(this.bills[i]));
87+
}
88+
},
89+
calcTotals: function() {
90+
for (var i = 0; i < this.bills.length; i++) {
91+
this.totals.push(this.bills[i] + this.tips[i]);
92+
}
93+
}
94+
}
95+
mark.calcAllTips();
96+
mark.calcTotals();
97+
console.dir(mark);
98+
99+
function calcAverage(tips) {
100+
var sum = 0;
101+
for (var i = 0; i < tips.length; i++) {
102+
sum += tips[i];
103+
}
104+
return sum / tips.length;
105+
}
106+
107+
john.average = calcAverage(john.tips);
108+
console.log(`${john.fullName}'s average tips = ${john.average}`);
109+
110+
mark.average = calcAverage(mark.tips);
111+
console.log(`${mark.fullName}'s average tips = ${mark.average}`);
112+
113+
if (john.average > mark.average) {
114+
console.log(`${john.fullName} paid the highest tips on average`)
115+
} else if (mark.average > john.average) {
116+
console.log(`${mark.fullName} paid the highest tips on average`)
117+
} else {
118+
console.log(`${john.fullName} and ${mark.fullName} paid the same tips on average`)
119+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /