Skip to content

Commit f68d570

Browse files
committed
Tip calculator completed
1 parent b8b0eb4 commit f68d570

3 files changed

Lines changed: 68 additions & 5 deletions

File tree

‎Week3/homework/project-TipCalculator/index.html‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ <h1>TIP CALCULATOR</h1>
2323
<span>€ </span><input type="number" id="bill">
2424
<p>How was your service?</p>
2525
<select id="services">
26+
<option value="choose-option">-Choose an option-</option>
2627
<option value="outstanding">30% - Outstanding</option>
2728
<option value="good">20% - Good</option>
2829
<option value="ok">15% - It was OK</option>
@@ -32,17 +33,16 @@ <h1>TIP CALCULATOR</h1>
3233
<p>How many people are sharing the bill?</p>
3334
<input type="number" id="people"><span>people</span>
3435
<br>
35-
<button id="calc-button">CALCULATE!</button>
36+
<button type="button" id="calc-button">CALCULATE!</button>
3637
<h3>TIP AMOUNT</h3>
37-
<h4 id="tip-amount"></h4>
38-
<h2>€ 0.00</h2>
39-
<h3>each</h2>
38+
<h2 id="tip-amount">€ 0.00</h2>
39+
<h3 id="each">each</h3>
4040
</form>
4141
</div>
4242
</div>
4343
</div>
4444

45-
<script src="script.js"></script>
45+
<script src="main.js"></script>
4646
</body>
4747

4848
</html>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"use strict"
2+
//Selecting the elements
3+
4+
const bill = document.querySelector("#bill");
5+
const services = document.querySelector("#services");
6+
const people = document.querySelector("#people");
7+
const button = document.querySelector("#calc-button");
8+
const amount = document.querySelector("#tip-amount");
9+
const each = document.querySelector("#each");
10+
let total;
11+
12+
//Event listener for the button click
13+
button.addEventListener("click", calculateTips);
14+
15+
//Function that calculates the tips for each service selection
16+
function calculateTips() {
17+
//Checks if some of the fields is blank
18+
if (bill.value === "" || services.value === "choose-option" || people.value === "") {
19+
alert("You need to fill in all the fields!");
20+
//Changes the tip value for each percentage
21+
} else {
22+
switch (services.value) {
23+
case "outstanding":
24+
onePerson();
25+
total = (bill.value * 0.30) / people.value;
26+
amount.innerHTML = `€ ${total}`;
27+
break;
28+
case "good":
29+
onePerson();
30+
total = (bill.value * 0.20) / people.value;
31+
amount.innerHTML = `€ ${total}`;
32+
break;
33+
case "ok":
34+
onePerson();
35+
total = (bill.value * 0.15) / people.value;
36+
amount.innerHTML = `€ ${total}`;
37+
break;
38+
case "bad":
39+
onePerson();
40+
total = (bill.value * 0.1) / people.value;
41+
amount.innerHTML = `€ ${total}`;
42+
break;
43+
case "terrible":
44+
onePerson();
45+
total = (bill.value * 0.05) / people.value;
46+
amount.innerHTML = `€ ${total}`;
47+
break;
48+
}
49+
}
50+
}
51+
52+
//Function that checks if there is more than one person that pays the bill and adds or deletes the "each"
53+
function onePerson() {
54+
if (parseInt(people.value) === 1) {
55+
each.innerHTML = "";
56+
} else {
57+
each.innerHTML = "each";
58+
}
59+
}

‎Week3/homework/project-TipCalculator/style.css‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ div.calc-body{
7171
font-family: 'Odibee Sans', cursive;
7272
cursor: pointer;
7373
}
74+
#calc-button:hover {
75+
background-color: #fd7f7f;
76+
}
77+
7478
input#bill{
7579
font-family: 'Segoe UI', sans-serif;
7680
font-size: 1.3rem;

0 commit comments

Comments
 (0)