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

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

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, not needed for Complete: min is turned into a number here, and then on line 14 it is turned back into a string and padded with a 0. If you kept it as a string, time.slice(-2) already gives you "04" with the 0 in place. Then you wouldn't need .toString().padStart(...) for the minutes.

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.

I had it showing up as "4" a few times when I was doing a test so I just put that in to ensure it was working, might have messed up something on my end to make it do that.

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.

That makes sense: Number("04") gives 4, which is why you needed the padStart. It works as it is, so leave it.


if (hours > 12) {
return `${hours - 12}:00 pm`;
if (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.

You've handled the 12 hour here. The other special hour is 00, the first hour after midnight.

At the moment formatAs12HourClock("00:00") falls through to the last line and returns "00:00 am". On a 12-hour clock midnight is written as 12:00 am, and half past midnight as 12:30 am.

Hint: add another check like the one on this line, but for hours === 0. Inside it, return a string that starts with 12: and ends with am. You already have the minutes in min.

When it works you should get:

  • formatAs12HourClock("00:00") gives "12:00 am"
  • formatAs12HourClock("00:30") gives "12:30 am"
  • formatAs12HourClock("01:00") still gives "01:00 am"

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.

Ah I understand, I have done the changes for this so it should be seen now in the code.

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.

That's it. 00:00 and 00:30 both come out right now. Well done, this was the trickiest bit.

return `${time} pm`;
} else if (hours === 0) {
return `${(hours + 12).toString().padStart(2, "0")}:${min.toString().padStart(2, "0")} am`;
} else if (hours > 12) {
return `${(hours - 12).toString().padStart(2, "0")}:${min.toString().padStart(2, "0")} pm`;
}
return `${time} am`;
}

export {formatAs12HourClock};
export { formatAs12HourClock };
48 changes: 43 additions & 5 deletions format-clock-edge-cases/timeConverter.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,49 @@
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");
assert.equal(formatAs12HourClock("01:00"), "01:00 am");
});

test("can correctly convert time with minutes for am", function () {
assert.equal(formatAs12HourClock("08:35"), "08:35 am");
});

test("can correctly convert time with minutes for pm", function () {
assert.equal(formatAs12HourClock("18:35"), "06:35 pm");
});

test("can correctly convert time with minutes with 0 pad such as 13:04", function () {
assert.equal(formatAs12HourClock("13:04"), "01:04 pm");
});

test("can correctly convert time with hour being 00 to show as 12 am", function () {
assert.equal(formatAs12HourClock("00:00"), "12:00 am");
assert.equal(formatAs12HourClock("00:30"), "12:30 am");
});

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

test("last minute before 12 pm", function () {
assert.equal(formatAs12HourClock("11:59"), "11:59 am");
});
test("noon is 12 pm", function () {
assert.equal(formatAs12HourClock("12:00"), "12:00 pm");
});
test("first minute after noon", function () {
assert.equal(formatAs12HourClock("12:01"), "12:01 pm");
});
test("first hour after noon is 1 pm", function () {
assert.equal(formatAs12HourClock("13:00"), "01:00 pm");
});
test("last minute before midnight", function () {
assert.equal(formatAs12HourClock("23:59"), "11:59 pm");
});
Loading