Skip to content

Commit f8a6c20

Browse files
committed
updated the 2 files
1 parent 29ef74a commit f8a6c20

2 files changed

Lines changed: 49 additions & 10 deletions

File tree

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
function formatAs12HourClock(time) {
2-
32
const hours = Number(time.slice(0, 2));
3+
const minutes = time.slice(-2);
44

5-
if (hours > 12) {
6-
return `${hours - 12}:00 pm`;
5+
if (time == "00") {
6+
return `12:00:${minutes} am`;
7+
} else if (hours > 12) {
8+
return `${hours - 12}:${minutes} pm`;
9+
} else if (hours === 12) {
10+
return `12:${minutes} pm`;
11+
} else {
12+
return `${time} am`;
713
}
8-
return `${time} am`;
914
}
1015

11-
export {formatAs12HourClock};
16+
console.log(formatAs12HourClock("23:00"));
17+
console.log(formatAs12HourClock("14:00"));
18+
console.log(formatAs12HourClock("12:00"));
19+
console.log(formatAs12HourClock("08:15"));
20+
console.log(formatAs12HourClock("00:00"));
21+
22+
export { formatAs12HourClock };
Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
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");
11+
});
12+
13+
test("can correctly convert midday", function () {
14+
assert.equal(formatAs12HourClock("12:00"), "12:00 pm");
15+
});
16+
17+
test("can correctly convert midnight", function () {
18+
assert.equal(formatAs12HourClock("00:00"), "12:00 am");
19+
});
20+
21+
test("can correctly convert 2 hours before 12:00", function () {
22+
assert.equal(formatAs12HourClock("10:00"), "10:00 am");
23+
});
24+
25+
test("can correctly convert 30 minutes before 12:00", function () {
26+
assert.equal(formatAs12HourClock("11:30"), "11:30 am");
27+
});
28+
29+
test("can correctly convert time at the beginning of noon", function () {
30+
assert.equal(formatAs12HourClock("12:001"), "12:01 pm");
31+
});
32+
33+
test("can correctly convert half hour before midnight", function () {
34+
assert.equal(formatAs12HourClock("23:30"), "11:30 pm");
35+
});
36+
37+
test("can correctly convert 1 minute before midnight", function () {
38+
assert.equal(formatAs12HourClock("23:59"), "11:59 pm");
1139
});

0 commit comments

Comments
 (0)