-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
executable file
·83 lines (70 loc) · 2.9 KB
/
app.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
const percentageSelect = document.getElementById("percentage");
const presentInput = document.getElementById("present-input");
const totalInput = document.getElementById("total-input");
const btn = document.getElementById("btn");
const outputDiv = document.getElementById("output-div");
const footer = document.getElementById("footer");
const banner = document.getElementById("banner");
btn.addEventListener("click", () => {
let present = parseInt(presentInput.value);
let total = parseInt(totalInput.value);
let percentage = parseInt(percentageSelect.value);
if (present < 0 || total <= 0 || present > total) {
return (outputDiv.innerText = "Proper values please ¯\\_(ツ)_/¯");
}
if (present / total >= percentage / 100) {
const daysAvailableToBunk = daysToBunk(present, total, percentage);
return (outputDiv.innerHTML = daysToBunkText(
daysAvailableToBunk,
present,
total
));
}
const attendanceNeeded = reqAttendance(present, total, percentage);
return (outputDiv.innerHTML = daysToAttendClassText(
attendanceNeeded,
present,
total,
percentage
));
});
const reqAttendance = (present, total, percentage) => {
return Math.ceil((percentage * total - 100 * present) / (100 - percentage));
};
const daysToBunk = (present, total, percentage) => {
return Math.floor((100 * present - percentage * total) / percentage);
};
const daysToBunkText = (daysAvailableToBunk, present, total) =>
`You can bunk for <strong>${daysAvailableToBunk}</strong> more days.<br>Current Attendance: <strong>${present}/${total}</strong> -> <strong>${(
(present / total) *
100
).toFixed(2)}%</strong><br>Attendance Then: <strong>${present}/${
daysAvailableToBunk + total
}</strong> -> <strong>${(
(present / (daysAvailableToBunk + total)) *
100
).toFixed(2)}%</strong>`;
const daysToAttendClassText = (attendanceNeeded, present, total, percentage) =>
`You need to attend <strong>${attendanceNeeded}</strong> more classes to attain ${percentage}% attendance<br>Current Attendance: <strong>${present}/${total}</strong> -> <strong>${(
(present / total) *
100
).toFixed(2)}%</strong><br>Attendance Required: <strong>${
attendanceNeeded + present
}/${attendanceNeeded + total}</strong> -> <strong>${(
((attendanceNeeded + present) / (attendanceNeeded + total)) *
100
).toFixed(2)}%</strong>`;
// Issue: Footer gets pushed up when the keyboard on mobile screen come up due to the absolute positioning of the footer
// Solution (Hacky Fix): Hide the footer when any of the input is focused
presentInput.addEventListener("focus", () => {
footer.classList.add("hide-footer");
});
presentInput.addEventListener("focusout", () => {
footer.classList.remove("hide-footer");
});
totalInput.addEventListener("focus", () => {
footer.classList.add("hide-footer");
});
totalInput.addEventListener("focusout", () => {
footer.classList.remove("hide-footer");
});