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

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

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

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.

This is why the midnight test fails.

Line 5 checks whether time is "00". But for midnight, time is the whole text "00:00", so the check is never true, and the code carries on down to line 12, which returns "00:00 am".

Line 2 already gives you the hour on its own, as a number, in the variable hours. Change line 5 so it checks hours instead of time. Remember hours is a number, so compare it with a number, not with text in quotes.

Then look at line 6. It returns 12:00: followed by the minutes, which gives 12:00:00 am, one part too many. Line 10 is how you did it for noon. Midnight needs the same shape, but with am instead of pm.

Run node --test timeConverter.test.js in the terminal again. The midnight test should now show ✔.

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

export {formatAs12HourClock};
console.log(formatAs12HourClock("23:00"));

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.

These five lines were useful while you were working it out, but they print every time the tests run: you can see 11:00 pm, 2:00 pm and so on above your test results. Delete lines 16 to 20, then run node --test timeConverter.test.js again to check nothing else changed.

console.log(formatAs12HourClock("14:00"));
console.log(formatAs12HourClock("12:00"));
console.log(formatAs12HourClock("08:15"));
console.log(formatAs12HourClock("00:00"));

export { formatAs12HourClock };
38 changes: 33 additions & 5 deletions format-clock-edge-cases/timeConverter.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
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("correctly convert time after 12:00", function () {
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 correctly convert morning time", function () {
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
});

test("can correctly convert midday", function () {
assert.equal(formatAs12HourClock("12:00"), "12:00 pm");
});

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

test("can correctly convert 2 hours before 12:00", function () {
assert.equal(formatAs12HourClock("10:00"), "10:00 am");
});

test("can correctly convert 30 minutes before 12:00", function () {
assert.equal(formatAs12HourClock("11:30"), "11:30 am");
});

test("can correctly convert time at the beginning of noon", function () {
assert.equal(formatAs12HourClock("12:001"), "12:01 pm");

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.

There's an extra 1 here: "12:001" should be "12:01". Delete the extra 1 so the test checks a real time. (It passes at the moment only by luck, because the function looks at the last two characters.)

});

test("can correctly convert half hour before midnight", function () {
assert.equal(formatAs12HourClock("23:30"), "11:30 pm");
});

test("can correctly convert 1 minute before midnight", function () {
assert.equal(formatAs12HourClock("23:59"), "11:59 pm");
});
Loading