Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions format-clock-edge-cases/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
In your prep, you looked this `formatAs12HourClock` function:

```js
function formatAs12HourClock(time) {

const hours = Number(time.slice(0, 2));

if (hours > 12) {
return `${hours - 12}:00 pm`;
}
return `${time} am`;
}
```

It still has bugs. Think of as many edge-cases as you can with this code. Write tests for all of them, and fix this code so that it works correctly for all valid inputs. You don't need to worry about invalid inputs (e.g. `"25:00"`).
11 changes: 11 additions & 0 deletions format-clock-edge-cases/timeConverter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function formatAs12HourClock(time) {

const hours = Number(time.slice(0, 2));

if (hours > 12) {
return `${hours - 12}:00 pm`;
}
return `${time} am`;
}

export {formatAs12HourClock};
11 changes: 11 additions & 0 deletions format-clock-edge-cases/timeConverter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {formatAs12HourClock} from "./timeConverter.js";
import assert from "node:assert";
import test from "node:test";

test("correctly convert time after 12:00", function(){
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
});

test("can correctly convert morning time", function() {
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
});
Loading