Skip to content

Commit f141efb

Browse files
committed
Add format clock edge cases exercise
1 parent 1ae8808 commit f141efb

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎format-clock-edge-cases/README.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
In your prep, you looked this `formatAs12HourClock` function:
2+
3+
```js
4+
function formatAs12HourClock(time) {
5+
6+
const hours = Number(time.slice(0, 2));
7+
8+
if (hours > 12) {
9+
return `${hours - 12}:00 pm`;
10+
}
11+
return `${time} am`;
12+
}
13+
```
14+
15+
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"`).
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function formatAs12HourClock(time) {
2+
3+
const hours = Number(time.slice(0, 2));
4+
5+
if (hours > 12) {
6+
return `${hours - 12}:00 pm`;
7+
}
8+
return `${time} am`;
9+
}
10+
11+
export {formatAs12HourClock};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {formatAs12HourClock} from "./timeConverter.js";
2+
import assert from "node:assert";
3+
import test from "node:test";
4+
5+
test("correctly convert time after 12:00", function(){
6+
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
7+
});
8+
9+
test("can correctly convert morning time", function() {
10+
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
11+
});

0 commit comments

Comments
 (0)