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

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

if (hours > 12) {
return `${hours - 12}:00 pm`;
if (hours > 21) {
return `${hours - 12}:${minutes} pm`;
} else if (hours > 12) {
return `0${hours - 12}:${minutes} pm`;
} else if (hours === 12) {
return `12:${minutes} pm`;
} else if (hours > 9) {
return `${hours}:${minutes} 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.

Lines 10 and 14 return the same template. Is there any input that reaches line 10 and would give a different answer if it went to line 14 instead? If not, do you need this branch?

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.

Subsequent to put back starter tests expecting results (0X:00 am/pm) on single hour scenarios, keep existing branches to distinct where hours >12 (for pm) and hours <=9 (for am).

Also added hours >21 branch to keep 10pm and beyond in place rather than showing 010:00pm. Validated and passed in the first test case in test.js

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.

Agreed, the two branches give different answers now.

} else if (hours < 1) {
return `12:${minutes} am`;
}
return `${time} am`;
return `0${hours}:${minutes} am`;
}

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

Your 00:00 and 12:00 tests both use 00 minutes. What happens with 00:30 or 12:01? Each goes through a branch that builds the minutes on its own. And 13:00 is the very first time that reaches your hours > 12 branch. Each of those is worth its own test.

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.

Added additional test cases for last minute of morning, last minute of the day, first minute of the day, and first minute after noon.

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.

All tests passed.

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.

All three are there now. Good.

});

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

test("can correctly convert dual characters hour morning time", function () {
assert.equal(formatAs12HourClock("11:00"), "11:00 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.

Bugs often hide at the exact point where something changes. 11:00 is an hour before am turns into pm. What is the very last minute before that happens? And what is the last minute of the whole day, just before it goes back to am? Each of those is worth its own test.

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.

Added two test cases for edge scenarios: last minutes in each session of the day.

Both tests passed.

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.

Those are the two. Good.

});

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

test("can correctly convert to single character afternoon time", function () {

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.

Lines 29 and 33 both call formatAs12HourClock("16:59") and expect the same answer. What does the second one check that the first doesn't? Keep one, or change one to an input that tests something new.

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.

Changed the hour in second test on line 33 - to test minutes input shows correctly.

Test passed.

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.

13:30 tests something new. Good.

assert.equal(formatAs12HourClock("16:00"), "04:00 pm");
});

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

test("can correctly shows minutes input", function () {
assert.equal(formatAs12HourClock("13:30"), "01:30 pm");
});

test("can correctly keep am in last minute of morning", function () {
assert.equal(formatAs12HourClock("11:59"), "11:59 am");
});

test("can correctly keep pm in last minute of the day", function () {
assert.equal(formatAs12HourClock("23:59"), "11:59 pm");
});

test("can correctly keep am in first minute of the day", function () {
assert.equal(formatAs12HourClock("00:01"), "12:01 am");
});

test("can correctly keep pm in first minute after noon", function () {
assert.equal(formatAs12HourClock("12:01"), "12:01 pm");
});
Loading