-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (30 loc) · 1.04 KB
/
index.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
$(document).ready(function() {
const timestamp = moment().format('MMM D, YYYY h:mm:ss A');
$('#timestamp').html(timestamp);
$('#bill_amount').keyup(function() {
calculateTip();
});
$('#tip_percentage').keyup(function() {
calculateTip();
});
function calculateTip() {
const bill_amount_field = $('#bill_amount');
const tip_pct_field = $('#tip_percentage');
const tip_amount_field = $('#tip_amount');
const grand_total_field = $('#grand_total');
const bill_amount = parseFloat(bill_amount_field.val());
const tip_percentage = parseFloat(tip_pct_field.val()) * 0.01;
if (isNaN(bill_amount) || isNaN(tip_percentage)) {
tip_amount_field.empty();
grand_total_field.empty();
return;
}
const tip_amount = bill_amount * tip_percentage;
const grand_total = bill_amount + tip_amount;
tip_amount_field.text(displayInDollars(tip_amount));
grand_total_field.text(displayInDollars(grand_total));
}
function displayInDollars(float) {
return '$' + Number(float).toFixed(2);
}
});