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
24 changes: 19 additions & 5 deletions format-clock-edge-cases/timeConverter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
function formatAs12HourClock(time) {

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

let period = "";
let hourString = "";

if (hours > 12) {
return `${hours - 12}:00 pm`;
if (hours <= 11){
period = "am";
} else {
period = "pm";
}
return `${time} am`;

if (hours >= 13){
hourString = hours - 12 < 10 ? `0${hours - 12}` : `${hours -12}`;
} else if (hours === 0){
hourString = `${hours + 12}`;

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.

Optional: when hours is 0, what is hours + 12 always going to be? Would writing that value directly make this branch easier to read?

} else {
hourString = hours < 10 ? `0${hours}`:`${hours}`;
}

return `${hourString}:${minutes} ${period}`
}

export {formatAs12HourClock};
export { formatAs12HourClock };
27 changes: 20 additions & 7 deletions format-clock-edge-cases/timeConverter.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
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 format time after 12:00", () =>
assert.equal(formatAs12HourClock("23:00"), "11:00 pm"));

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

test("can format noon time", () =>
assert.equal(formatAs12HourClock("12:00"), "12:00 pm"));

test("can format afternoon time with minutes other than 00", () =>
assert.equal(formatAs12HourClock("15:45"), "03:45 pm"));

test("can format morning time with minutes other than 00", () =>
assert.equal(formatAs12HourClock("08:25"), "08:25 am"));

test("can format early noon with minutes other than 00", () =>
assert.equal(formatAs12HourClock("12:17"), "12:17 pm"));

test("can format time between midnight and 1 am", () =>

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.

00:15 checks the midnight hour, but not midnight itself. Which input is the very first minute of the day, which are the last minute before noon and the last minute before midnight, and which is the first time your hours >= 13 branch handles? Those are the places an if on hours is most likely to go wrong, so each is worth its own test.

assert.equal(formatAs12HourClock("00:15"), "12:15 am"));
Loading