-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
96 lines (91 loc) · 2.96 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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// people input-
let totalBill = 1;
let totalPeople = 1;
let isGiveTip = false;
let isTipClick = false;
let rate = 0;
let totalTips = 0;
let tipButtonElement;
const tipElement = document.getElementById("tip-amount");
const totalPeopleElement = document.getElementById("total-poeple");
// input functionality-
const startInput = (getElement) => {
const getValue = parseInt(getElement.value);
totalPeopleElement.innerText = getValue;
if (getElement.name == "bill") {
tipElement.innerText = parseInt(totalTips / totalPeople);
return (totalBill = getValue);
} else if (getElement.name == "people") {
tipElement.innerText = parseInt(totalTips / totalPeople);
return (totalPeople = getValue);
}
};
function peopleInput(id) {
document.getElementById(id).addEventListener("input", (event) => {
startInput(event.target);
tipElement.innerText = parseInt(totalTips / totalPeople);
totalPeopleElement.innerText = parseInt(totalBill / totalPeople);
totalBill = totalBill / totalPeople;
// console.log(totalBill, totalPeople);
if (isTipClick == true) {
let resultTips = tipsAmount();
console.log(resultTips);
isTipClick == false;
}
isGiveTip = true;
TipButtons();
});
}
peopleInput("bill-input");
peopleInput("people-input");
// tips rate functionality common function-
const tipsAmount = () => {
totalTips = (totalBill * rate) / 100;
// tipElement.innerText = tipsMoney;
// tipButtonElement.setAttribute("disabled", true);
// console.log(totalBill);
return totalTips;
};
// select tip functionality-
const TipButtons = () => {
document.getElementById("tipBtn-box").addEventListener("click", (event) => {
isTipClick = true;
const tipButtonInnerText = event.target.innerText;
tipButtonElement = event.target;
if (isGiveTip) {
if (tipButtonInnerText == "5%") {
rate = 5;
tipsAmount();
tipElement.innerText = totalTips;
totalPeopleElement.innerText = totalBill + totalTips;
console.log(totalTips);
} else if (tipButtonInnerText == "10%") {
rate = 10;
tipsAmount();
tipElement.innerText = totalTips;
totalPeopleElement.innerText = totalBill + totalTips;
console.log(totalTips);
} else if (tipButtonInnerText == "15%") {
rate = 15;
tipsAmount();
tipElement.innerText = totalTips;
totalPeopleElement.innerText = totalBill + totalTips;
console.log(totalTips);
} else if (tipButtonInnerText == "25%") {
rate = 25;
tipsAmount();
tipElement.innerText = totalTips;
totalPeopleElement.innerText = totalBill + totalTips;
console.log(totalTips);
} else if (tipButtonInnerText == "50%") {
rate = 50;
tipsAmount();
tipElement.innerText = totalTips;
totalPeopleElement.innerText = totalBill + totalTips;
console.log(totalTips);
}
} else {
console.log("plese input your bill first!");
}
});
};