Skip to content

Commit 7a4a0c7

Browse files
committed
Implement formatAs12HourClock function with comprehensive tests for time conversion
1 parent 1c195d7 commit 7a4a0c7

3 files changed

Lines changed: 87 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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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};
12+
13+
function formatAs12HourClock(time){
14+
const [hourPart, minutePart] = time.split(":");
15+
const hours = Number(hourPart);
16+
17+
let period;
18+
if(hours < 12){
19+
period = "am";
20+
} else {
21+
period = "pm";
22+
}
23+
24+
let displayHour = hours % 12;
25+
if(displayHour === 0){
26+
displayHour = 12;
27+
}
28+
return `${displayHour}:${minutePart}${period}`;
29+
}
30+
export {formatAs12HourClock};
31+
console.log(formatAs12HourClock("08:40"))
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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", () => {
6+
assert.equal(formatAs12HourClock("23:00"), "11:00pm");
7+
});
8+
9+
test("converts a single-digit morning hour and drops leading zero", () => {
10+
assert.equal(formatAs12HourClock("08:00"), "8:00am");
11+
})
12+
test("converts exactly to midnight", () => {
13+
assert.equal(formatAs12HourClock("00:00"), "12:00am");
14+
})
15+
test("converts time just after midnight", () => {
16+
assert.equal(formatAs12HourClock("00:30"), "12:30am");
17+
})
18+
test("converts the last minute before noon", () => {
19+
assert.equal(formatAs12HourClock("11:59"), "11:59am");
20+
})
21+
test("converts exact noon to pm", () => {
22+
assert.equal(formatAs12HourClock("12:00"), "12:00pm");
23+
})
24+
test("convert time just after noon", () => {
25+
assert.equal(formatAs12HourClock("12:30"), "12:30pm");
26+
})
27+
test("converts the first afternoon hour", () => {
28+
assert.equal(formatAs12HourClock("13:00"), "1:00pm");
29+
})
30+
test("converts late evening hour", () => {
31+
assert.equal(formatAs12HourClock("23:00"), "11:00pm");
32+
})
33+
test("converts last minute of the day", () => {
34+
assert.equal(formatAs12HourClock("23:59"), "11:59pm");
35+
})
36+
test("preserves minutes with leading zero", () => {
37+
assert.equal(formatAs12HourClock("09:05"), "9:05am");
38+
})
39+
test("preserves non-zero minutes", () => {
40+
assert.equal(formatAs12HourClock("14:45"), "2:45pm");
41+
})

0 commit comments

Comments
 (0)