-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
46 lines (33 loc) · 1.39 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// script.js
alert("hello");
// custom function
function calculateTip() {
// store the data of inputs
var billAmount = document.getElementById("billAmount").value;
var serviceQuality = document.getElementById("serviceQuality").value;
var numPeople = document.getElementById("totalPeople").value;
//Quick validation
if (billAmount === ""|| serviceQuality == 0) {
window.alert("please enter some values to get this baby up and running!");
return; // this will prevent the function from continuing
}
// check to see if this input is empty or less than or equal to 1
if(numPeople === "" || numPeople <= 1) {
numPeople = 1;
document.getElementById("each").style.display = "none";
} else {
document.getElementById("each").style.display = "block";
}
// do some math!
var total = (billAmount * serviceQuality) / numPeople;
total = Math.round(total * 100) / 100;
total = total.toFixed(2);
// Display the tip!
document.getElementById("totalTip").style.display = "block";
document.getElementById("tip").innerHTML = total;
}
// Hide the tip amount on load
document.getElementById("totalTip"). style.display = "none";
document.getElementById("each"). style. display = "none";
// clicking the button calls our custom function
document.getElementById("calculate").onclick = function() { calculateTip(); };