-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtip-calculator.js
82 lines (67 loc) · 2.54 KB
/
tip-calculator.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
function CalculateTips()
{
var Bill = document.getElementById("inputBill").value;
var Tips = document.getElementById("inputTips").value;
var Person = document.getElementById("inputPerson").value;
if(ValidateTipCalculatorForm(Bill,Tips,Person))
{
Bill = Number(Bill);
Tips = Number(Tips);
Person = Number(Person);
var TotalTips, TotalBill, PerPersonTips;
TotalTips = (Tips / 100) * Bill;
TotalBill = (TotalTips + Bill);
PerPersonTips =TotalBill / Person;
//show the results
ShowTipCalculationsResult("bill", (Bill.toFixed(2)));
ShowTipCalculationsResult("tip", Tips);
ShowTipCalculationsResult("person", Person);
ShowTipCalculationsResult("totalTips", (TotalTips.toFixed(2)));
ShowTipCalculationsResult("totalBill", (TotalBill.toFixed(2)));
ShowTipCalculationsResult("perPersonBill", (PerPersonTips.toFixed(2)));
//result div show
_cmnHideElement("OutputInfo");
_cmnShowElement("OutputResult", "flex");
}
}
function ResetTipCalculator()
{
if(confirm("Are you sure want to reset the calculator?")){
document.getElementById("inputBill").value = "";
document.getElementById("inputTips").value = "";
document.getElementById("inputPerson").value = "";
ShowTipCalculationsResult("bill", "0");
ShowTipCalculationsResult("tip", "0");
ShowTipCalculationsResult("person", "0");
ShowTipCalculationsResult("totalTips", "0");
ShowTipCalculationsResult("totalBill", "0");
ShowTipCalculationsResult("perPersonBill", "0");
_cmnRemoveAllErrorMessage();
_cmnHideElement("OutputResult");
_cmnShowElement("OutputInfo", "flex");
}
}
function ValidateTipCalculatorForm(bill,tips,person)
{
_cmnRemoveAllErrorMessage();
if(bill == "" || isNaN(bill) || bill <= 0)
{
_cmnShowErrorMessageBottomOfTheInputFiled("inputBill", "Enter the valid bill.");
return false;
}
if(tips == "" || isNaN(tips) || tips < 1)
{
_cmnShowErrorMessageBottomOfTheInputFiled("inputTips", "Enter the valid tips percentage.");
return false;
}
if(person == "" || isNaN(person) || !Number.isInteger(Number(person)) || person <= 0)
{
_cmnShowErrorMessageBottomOfTheInputFiled("inputPerson", "Enter the valid number of person.");
return false;
}
return true;
}
function ShowTipCalculationsResult(elementID, value)
{
document.getElementById(elementID).innerHTML = value;
}