Skip to content

Commit 45bf811

Browse files
committed
pre final (tip calculator project)
1 parent 51b2c29 commit 45bf811

3 files changed

Lines changed: 82 additions & 3 deletions

File tree

Week3/project/index.html

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,32 @@
44
<head>
55
<meta charset="UTF-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<link rel="stylesheet" href="style.css">
78
<title>Tip Calculator</title>
89
</head>
910

1011
<body>
11-
<div id="app"></div>
12+
<div id="app">
13+
<h1>Tip Calculator</h1>
14+
<form action="">
15+
<label for="amount">How much was your bill?</label>
16+
<span>$</span><input type="text" id="amount">
17+
<label for="service"></label>
18+
<select id="service">
19+
<option value="">How was your service?</option>
20+
<option value="30">30%</option>
21+
<option value="20">20%</option>
22+
<option value="15">15%</option>
23+
<option value="10">10%</option>
24+
<option value="5">5%</option>
25+
</select>
26+
<label for="sharing-people">How many people are sharing the bill?</label>
27+
<input type="text" id="sharingPeople">
28+
29+
</form>
30+
<button id="calculate-button">cal</button>
31+
<h2 id="result"></h2>
32+
</div>
1233
<script src="index.js"></script>
1334
</body>
1435

Week3/project/index.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
1-
// Your code goes in here
1+
// document.querySelector("#app").innerText = "Tip Calculator";
2+
const calculateButton = document.getElementById('calculate-button');
3+
const CalculateTip = () => {
4+
const amount = document.getElementById('amount');
5+
const service = document.getElementById('service');
6+
const sharingPeople = document.getElementById('sharingPeople');
7+
const result = document.getElementById('result');
8+
9+
let tipAmount = (parseFloat(amount.value) + (parseFloat(amount.value) / 100) * parseFloat(service.value)) / parseFloat(sharingPeople.value);
10+
let amountFlag = false;
11+
let sharingPeopleFlag = false;
12+
if (amount.value === '' || service.value === '' || sharingPeople.value === '') {
13+
alert('Fill in all the fields!')
14+
} else {
15+
if (!isNaN(amount.value)) {
16+
amountFlag = true
17+
} else {
18+
amount.value = `invalid Number`
19+
amount.style.color = 'red';
20+
}
21+
22+
if (!isNaN(sharingPeople.value)) {
23+
sharingPeopleFlag = true
24+
} else {
25+
sharingPeople.value = `invalid Number`
26+
sharingPeople.style.color = 'red';
27+
}
28+
if (amountFlag === true && sharingPeopleFlag === true) {
29+
if (parseFloat(sharingPeople.value) !== 1) {
30+
result.textContent = `TIP AMOUNT $${tipAmount} each`
31+
} else {
32+
result.textContent = `TIP AMOUNT $${tipAmount}`
33+
}
34+
35+
}
36+
}
37+
console.log(amount.value);
38+
}
39+
40+
calculateButton.addEventListener("click", CalculateTip);
241

3-
document.querySelector("#app").innerText = "Tip Calculator";

Week3/project/style.css

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
*{
2+
box-sizing: border-box;
3+
margin: 0;
4+
padding: 0;
5+
display: inline-block;
6+
}
7+
body{
8+
background-color: rgb(0, 0, 0);
9+
text-align: center;
10+
}
11+
#app{
12+
background-color: tomato;
13+
max-width: 20%;
14+
height: 300px;
15+
margin: 100px auto;
16+
17+
}
18+
form > *{
19+
margin: 5px 0;
20+
}

0 commit comments

Comments
 (0)