Skip to content

Commit c035213

Browse files
committed
fix: bug with unsorted dates
1 parent 4e6bd71 commit c035213

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
.prettierrc

TypeScript/TollCalculator.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ describe("TollCalculator", () => {
124124
expect(fee).toBe(16);
125125
});
126126

127+
test("It handles passing billings in unsorted order", () => {
128+
const fee = tollCalculator.getTotalTollFee(Vehicle.Car, [
129+
new Date(Date.UTC(1995, 11, 12, 9, 45)),
130+
new Date(Date.UTC(1995, 11, 12, 8, 40)),
131+
]);
132+
133+
expect(fee).toBe(16);
134+
});
135+
127136
test("In case there are multiple billings in one hour, it only bills the largest one", () => {
128137
const fee = tollCalculator.getTotalTollFee(Vehicle.Car, [
129138
new Date(Date.UTC(1995, 11, 12, 8, 20)),

TypeScript/TollCalculator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TollCalculator {
1919
let tempFee = null;
2020
let totalFee = 0;
2121

22-
dates.forEach((date: Date) => {
22+
dates.sort().forEach((date: Date) => {
2323
if (!this.isSameDay(date, startDate)) {
2424
throw "Billings must be in the same day.";
2525
}

0 commit comments

Comments
 (0)