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
18 changes: 14 additions & 4 deletions format-clock-edge-cases/timeConverter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
function formatAs12HourClock(time) {
const [hoursStr, minutesStr] = time.split(":");
const hours = Number(hoursStr);
const minutes = minutesStr ?? "00";
Comment on lines +2 to +4

@cjyuan cjyuan Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you walk me through how minutesStr ?? "00" is evaluated?

You probably overlooked how ?? works.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first expression preserves the empty string; the second replaces it

@cjyuan cjyuan Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How could minutesStr be an empty string? How is the code on line 4 different from

  const minutes = minutesStr;


const hours = Number(time.slice(0, 2));
if (hours === 0) {
return `12:${minutes} am`;
}

if (hours < 12) {
return `${String(hours).padStart(2, "0")}:${minutes} am`;
}

if (hours > 12) {
return `${hours - 12}:00 pm`;
if (hours === 12) {
return `12:${minutes} pm`;
}
return `${time} am`;

return `${String(hours - 12).padStart(2, "0")}:${minutes} pm`;
}

export {formatAs12HourClock};
21 changes: 18 additions & 3 deletions format-clock-edge-cases/timeConverter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,25 @@ 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("converts midnight correctly", function() {
assert.equal(formatAs12HourClock("00:00"), "12:00 am");
});

test("can correctly convert morning time", function() {
test("converts early morning times correctly", function() {
assert.equal(formatAs12HourClock("01:00"), "01:00 am");
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
assert.equal(formatAs12HourClock("11:59"), "11:59 am");
});

test("converts noon correctly", function() {
assert.equal(formatAs12HourClock("12:00"), "12:00 pm");
assert.equal(formatAs12HourClock("12:30"), "12:30 pm");
});

test("converts afternoon and evening times correctly", function() {
assert.equal(formatAs12HourClock("13:00"), "01:00 pm");
assert.equal(formatAs12HourClock("18:45"), "06:45 pm");
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
});


Loading