Skip to content

Commit a04a9b1

Browse files
committed
Add 3rd Challenge - Tip Calculator
1 parent b78bbf5 commit a04a9b1

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
3+
Level: Beginner
4+
5+
CODING CHALLENGE 3
6+
7+
8+
9+
10+
John and his family went on a holiday and went to 3 different restaurants.
11+
The bills were: $124, $48 and $268.
12+
13+
To tip the waiter a fair amount, John created a simple tip calculator
14+
(as a function).
15+
16+
He likes to tip:
17+
20% of the bill when the bill is less than $50,
18+
15% when the bill is between $50 and $200,
19+
and 10% if the bill is more than $200.
20+
21+
In the end, John would like to have 2 arrays:
22+
1) Containing all three tips (one for each bill)
23+
2) Containing all three final paid amounts (bill + tip).
24+
25+
(NOTE: To calculate 20% of a value, simply multiply it with 20/100 = 0.2)
26+
27+
28+
29+
SOLUTION 👇🏼
30+
31+
*/
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
// With comments:
43+
44+
45+
// Store the initial values that were given.
46+
47+
var bills = [124, 48, 268]; // in dollars $
48+
var tips = [20, 15, 10]; // in percentage %
49+
50+
51+
52+
// Declare a function to calculate the tip.
53+
// Pass 'billAmount' as a parameter.
54+
// Store the final tip amount in 'finalTip'.
55+
// Return 'finalTip'.
56+
57+
function tipCalculator(billAmount) {
58+
var finalTip = (billAmount * 20) / 100; // (bill total amount * 20% extra tip) / 100
59+
return finalTip;
60+
}
61+
62+
63+
64+
// Add a conditional is/ else if statement to calculate
65+
// tips for all three scenarios (20%, 25%, 10%).
66+
67+
function tipCalculator(billAmount) {
68+
var finalTip;
69+
70+
if (billAmount < 50) {
71+
finalTip = (billAmount * 20) / 100;
72+
} else if (billAmount >= 50 && billAmount < 200) {
73+
finalTip = (billAmount * 15) / 100;
74+
} else {
75+
finalTip = (billAmount * 10) / 100;
76+
}
77+
return finalTip;
78+
}
79+
80+
81+
82+
// Update the 'tips' variable by calling the 'tipCaculator' function.
83+
84+
// Add the 'bills' array values inside the 'tipCaculator' function
85+
// by referring to their index number.
86+
87+
88+
var bills = [124, 48, 268];
89+
var tips = [
90+
tipCalculator(bills[0]),
91+
tipCalculator(bills[1]),
92+
tipCalculator(bills[2])
93+
];
94+
95+
96+
97+
// Store the final amount value inside a new variable.
98+
99+
// We now have the final paid amounts (bill + tip),
100+
// referenced by their index number, for all three scenarios (20%, 25%, 10%).
101+
102+
103+
var finalAmount = [bills[0] + tips[0],
104+
bills[1] + tips[1],
105+
bills[2] + tips[2]];
106+
107+
108+
109+
110+
/* ------------------------------------ */
111+
112+
// Without comments:
113+
114+
115+
function tipCalculator(billAmount) {
116+
var finalTip;
117+
118+
if (billAmount < 50) {
119+
finalTip = (billAmount * 20) / 100;
120+
} else if (billAmount >= 50 && billAmount < 200) {
121+
finalTip = (billAmount * 15) / 100;
122+
} else {
123+
finalTip = (billAmount * 10) / 100;
124+
}
125+
return finalTip;
126+
}
127+
128+
129+
var bills = [124, 48, 268];
130+
var tips = [
131+
tipCalculator(bills[0]),
132+
tipCalculator(bills[1]),
133+
tipCalculator(bills[2])
134+
];
135+
136+
var finalAmount = [bills[0] + tips[0],
137+
bills[1] + tips[1],
138+
bills[2] + tips[2]];
139+
140+

0 commit comments

Comments
 (0)