-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
61 lines (49 loc) · 2.45 KB
/
scripts.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
function calculate() {
const hourlyRates = {
islington: 22,
gosfordWeekday: 27,
gosfordWeekend: 30
};
const fuelCost = 30;
const days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"];
let totalHoursIslington = 0;
let totalHoursGosfordWeekdays = 0;
let totalHoursGosfordWeekends = 0;
let totalFuelCost = 0;
days.forEach(day => {
const startTime = document.getElementById(day + 'Start').value;
const endTime = document.getElementById(day + 'End').value;
const location = document.getElementById(day + 'Location').value;
if (!startTime || !endTime || !location) {
return; // skip this day if any value is missing
}
const start = new Date(`1970-01-01T${startTime}Z`);
const end = new Date(`1970-01-01T${endTime}Z`);
if (end < start) {
end.setDate(end.getDate() + 1); // account for working past midnight
}
const hoursWorked = (end - start) / (1000 * 60 * 60);
if (location === "islington") {
totalHoursIslington += hoursWorked;
} else if (day === "friday" || day === "saturday") {
totalHoursGosfordWeekends += hoursWorked;
totalFuelCost += fuelCost;
} else {
totalHoursGosfordWeekdays += hoursWorked;
totalFuelCost += fuelCost;
}
});
const earningsIslington = hourlyRates.islington * totalHoursIslington;
const earningsGosfordWeekday = hourlyRates.gosfordWeekday * totalHoursGosfordWeekdays;
const earningsGosfordWeekend = hourlyRates.gosfordWeekend * totalHoursGosfordWeekends;
const totalEarnings = earningsIslington + earningsGosfordWeekday + earningsGosfordWeekend - totalFuelCost;
const output = `
Total Hours worked in Islington: ${totalHoursIslington.toFixed(2)} - Earnings: $${earningsIslington.toFixed(2)}<br>
Total Hours worked in Gosford on Weekdays: ${totalHoursGosfordWeekdays.toFixed(2)} - Earnings: $${earningsGosfordWeekday.toFixed(2)}<br>
Total Hours worked in Gosford on Weekends: ${totalHoursGosfordWeekends.toFixed(2)} - Earnings: $${earningsGosfordWeekend.toFixed(2)}<br>
Total Fuel Cost for Gosford: -$${totalFuelCost.toFixed(2)}<br>
----------------------------<br>
Overall Earnings: $${totalEarnings.toFixed(2)}
`;
document.getElementById('output').innerHTML = output;
}