Skip to content
Open
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
13 changes: 10 additions & 3 deletions format-clock-edge-cases/timeConverter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
function formatAs12HourClock(time) {

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

const minutes = time.slice(3, 5);

if (hours === 0) {
return `12:${minutes} am`;
}
if (hours === 12) {
return `12:${minutes} pm`;
}
if (hours > 12) {
return `${hours - 12}:00 pm`;
return `${String(hours - 12).padStart(2, "0")}:${minutes} pm`;
}
return `${time} am`;
}

export {formatAs12HourClock};
export { formatAs12HourClock };
65 changes: 60 additions & 5 deletions format-clock-edge-cases/timeConverter.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,66 @@
import {formatAs12HourClock} from "./timeConverter.js";
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 time last minute of the day", function () {
assert.equal(formatAs12HourClock("23:59"), "11:59 pm");
});

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

test("can correctly convert time at midnight ", function () {
assert.equal(formatAs12HourClock("00:00"), "12:00 am");
});

test("can correctly convert time just after midnight", function () {
assert.equal(formatAs12HourClock("00:01"), "12:01 am");
});

test("can correctly convert time at the first normal hour", function () {
assert.equal(formatAs12HourClock("01:00"), "01:00 am");
});

test("can correctly convert time on leading zeros in hour and minute", function () {
assert.equal(formatAs12HourClock("09:05"), "09:05 am");
});

test("can correctly convert time just last minute before noon", function () {
assert.equal(formatAs12HourClock("11:59"), "11:59 am");
});

test("can correctly convert time to PM at noon", function () {
assert.equal(formatAs12HourClock("12:00"), "12:00 pm");
});

test("can correctly convert time just afternoon", function () {
assert.equal(formatAs12HourClock("12:01"), "12:01 pm");
});

test("can correctly convert time last minute of the noon hour", function () {
assert.equal(formatAs12HourClock("12:59"), "12:59 pm");
});

test("can correctly convert time first hour after noon", function () {
assert.equal(formatAs12HourClock("13:00"), "01:00 pm");
});

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

/*
Edge cases to be tested.
Input Expected What it tests
1. 00:00 -> 12:00 am Midnight: hour 0 becomes 12
2. 00:01 -> 12:01 am Just after midnight, still 12
3. 01:00 -> 01:00 am First normal morning hour
4. 09:05 -> 09:05 am Leading zeros in hour and minute
5. 11:59 -> 11:59 am Last minute before noon
6. 12:00 -> 12:00 pm Noon: switches to pm, hour stays 12
7. 12:01 -> 12:01 pm Just after noon, must not become 00
8. 12:59 -> 12:59 pm Last minute of the noon hour
9. 13:00 -> 01:00 pm First hour after noon: 13 - 12, padded
10.23:59 -> 11:59 pm Last minute of the day
*/
Loading