-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
72 lines (67 loc) · 2.54 KB
/
main.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//inputs
const billAmountInput = document.getElementById("billAmount");
const peopleNumberInput = document.getElementById("people");
//buttons
const tipBtns = document.querySelectorAll(".tipAmount");
const inputs = document.querySelectorAll(".input-num");
const customTipPercentage = document.getElementById("custom-tip");
const resetBtn = document.getElementById("reset");
//results
const tipResult = document.getElementById("tipPerPerson");
const totalResult = document.getElementById("totalPerPerson");
//error
const errorLabel = document.getElementById("people-error");
//button on click becomes active and passes its value
tipBtns.forEach(function(btn) {
btn.addEventListener("click", function(e) {
e.preventDefault();
tipBtns.forEach(function(button){
button.classList.remove("active");
});
e.target.classList.add("active");
const tipPercentage = parseInt(e.target.dataset.tip)/100;
calcTipForEveryone(tipPercentage);
});
});
customTipPercentage.addEventListener("input",function(){
const customTipValue = customTipPercentage.value;
const tipPercentage = customTipValue/100;
calcTipForEveryone(tipPercentage);
});
//reset button resets inputs, buttons
resetBtn.addEventListener("click", function() {
document.getElementById("form").reset();
tipBtns.forEach(function(button){
button.classList.remove("active");
});
tipResult.innerText = `$0.00`;
totalResult.innerText = `$0.00`;
errorLabel.classList.remove("error");
peopleNumberInput.classList.remove("error");
})
//when clicking on input its value refreshes
inputs.forEach(function(input) {
input.addEventListener("click", function(e) {
e.target.value = "";
});
});
//function calculates tip
function calcTipForEveryone(tipPercentage) {
const billAmount = parseFloat(billAmountInput.value);
const peopleNumber = parseInt(peopleNumberInput.value);
if (billAmount && peopleNumber) {
errorLabel.classList.remove("error");
peopleNumberInput.classList.remove("error");
let totalAmount = (billAmount * (1 + tipPercentage)) / peopleNumber;
totalAmount = Math.round(totalAmount * 100) / 100;
let tipAmount = (billAmount * tipPercentage) / peopleNumber;
tipAmount = Math.round(tipAmount * 100) / 100;
console.log(totalAmount, tipAmount);
tipResult.innerText = `$${tipAmount}`;
totalResult.innerText = `$${totalAmount}`;
}
if(!peopleNumber){
errorLabel.classList.add("error");
peopleNumberInput.classList.add("error");
}
};