Skip to content

Commit d667ab7

Browse files
committed
time converter exercise
1 parent 29ef74a commit d667ab7

2 files changed

Lines changed: 61 additions & 8 deletions

File tree

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
function formatAs12HourClock(time) {
2-
32
const hours = Number(time.slice(0, 2));
3+
const minutes = time.slice(3);
4+
5+
if (hours === 0) {
6+
return `12:${minutes} am`;
7+
}
8+
9+
if (hours === 12) {
10+
return `12:${minutes} pm`;
11+
}
412

513
if (hours > 12) {
6-
return `${hours - 12}:00 pm`;
14+
return `${hours - 12}:${minutes} pm`;
715
}
16+
817
return `${time} am`;
918
}
1019

11-
export {formatAs12HourClock};
20+
export { formatAs12HourClock };
Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,55 @@
1-
import {formatAs12HourClock} from "./timeConverter.js";
1+
import { formatAs12HourClock } from "./timeConverter.js";
22
import assert from "node:assert";
33
import test from "node:test";
44

5-
test("correctly convert time after 12:00", function(){
6-
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
5+
test("correctly convert time after 12:00", function () {
6+
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
77
});
88

9-
test("can correctly convert morning time", function() {
10-
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
9+
test("can correctly convert morning time", function () {
10+
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
1111
});
12+
13+
test("correctly convert time after 12:00", function () {
14+
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
15+
});
16+
17+
test("can correctly convert morning time", function () {
18+
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
19+
});
20+
21+
test("can correctly convert midnight", function () {
22+
assert.equal(formatAs12HourClock("00:00"), "12:00 am");
23+
});
24+
25+
test("can correctly convert midday", function () {
26+
assert.equal(formatAs12HourClock("12:00"), "12:00 pm");
27+
});
28+
29+
test("can correctly convert 12:30 pm", function () {
30+
assert.equal(formatAs12HourClock("12:30"), "12:30 pm");
31+
});
32+
33+
test("preserves minutes in the morning", function () {
34+
assert.equal(formatAs12HourClock("09:45"), "09:45 am");
35+
});
36+
37+
test("preserves minutes in the afternoon", function () {
38+
assert.equal(formatAs12HourClock("13:45"), "1:45 pm");
39+
});
40+
41+
test("correctly converts 1:00 pm", function () {
42+
assert.equal(formatAs12HourClock("13:00"), "1:00 pm");
43+
});
44+
45+
test("correctly converts 6:30 pm", function () {
46+
assert.equal(formatAs12HourClock("18:30"), "6:30 pm");
47+
});
48+
49+
test("correctly converts 11:59 pm", function () {
50+
assert.equal(formatAs12HourClock("23:59"), "11:59 pm");
51+
});
52+
53+
test("correctly converts 11:59 am", function () {
54+
assert.equal(formatAs12HourClock("11:59"), "11:59 am");
55+
});

0 commit comments

Comments
 (0)