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
79 changes: 74 additions & 5 deletions format-clock-edge-cases/timeConverter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,80 @@
function formatAs12HourClock(time) {
function pad(num) {
let stringNum = num.toString();
if (stringNum.length < 2) {
stringNum = "0" + stringNum;
return stringNum;
} else {
return stringNum;
}
}
function formatAs12HourClock(time = "") {
if (typeof time != "string") {
return "Not a valid format"; //check if time is a string
}

if (/\s/.test(time)) {
return "Not a valid time"; //check for whitespace in string
}

if (
(time.indexOf(":") === -1 && time.indexOf(".") === -1) ||
(time.indexOf(":") !== -1 && time.indexOf(".") !== -1)
) {
return "Not a valid time"; // check if there is only either one : or .
}

if (time.indexOf(":") === -1) {
if (time.indexOf(".") !== time.lastIndexOf(".")) {
return "Not a valid time"; //check there is only one .
}
}
if (time.indexOf(".") === -1) {
if (time.indexOf(":") !== time.lastIndexOf(":")) {
return "Not a valid time"; //check if there is only one :
}
}

let colonPeriodIndex = 0;
if (time.indexOf(":") === -1) {
colonPeriodIndex = time.indexOf(".");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What does this line do and why is it needed?

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.

In this line I get the index of the time separator. First I initialise colonPeriodIndex to 0, then I check if the ":" separator is not in the input string, and then set colonPeriodIndex to the index of "." . It's because I chose to accept "." as a valid separator.

} else {
colonPeriodIndex = time.indexOf(":");
}

const hours = Number(time.slice(0, 2));
if (
time.slice(0, colonPeriodIndex) === "" ||
time.slice(colonPeriodIndex + 1) === ""
) {
return "Not a valid time"; //check if there is a number before and after time separator
}
const hours = Number(time.slice(0, colonPeriodIndex));
const minutes = Number(time.slice(colonPeriodIndex + 1));
const stringMinutes = pad(minutes);
const stringHours = pad(hours);

if (
isNaN(hours) ||
isNaN(minutes) ||
hours >= 24 ||
hours < 0 ||
minutes >= 60 ||
minutes < 0 ||
hours % 1 != 0 ||
minutes % 1 != 0
) {
return "Not a valid time";
} //check if numbers are valid
if (hours === 12) {
return `${stringHours}:${stringMinutes} pm`;
}
if (hours === 0) {
return `12:${stringMinutes} am`;
}
if (hours > 12) {
return `${hours - 12}:00 pm`;
return `${pad(hours - 12)}:${stringMinutes} pm`;
}
return `${time} am`;

return `${stringHours}:${stringMinutes} am`;
}

export {formatAs12HourClock};
export { formatAs12HourClock };
134 changes: 129 additions & 5 deletions format-clock-edge-cases/timeConverter.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,135 @@
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 time after 12 with minutes that are not 00", function () {
assert.equal(formatAs12HourClock("23:46"), "11:46 pm");
});

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

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

test("can correctly reject 24:00 invalid", function () {
assert.equal(formatAs12HourClock("24:00"), "Not a valid time");
});

test("can correctly reject times with hours above 23 as invalid", function () {
assert.equal(formatAs12HourClock("47:00"), "Not a valid time");
});

test("can correctly reject times with minutes above 60 as invalid", function () {
assert.equal(formatAs12HourClock("15:75"), "Not a valid time");
});

test("can correctly reject times with minutes equal to 60 as invalid", function () {
assert.equal(formatAs12HourClock("15:60"), "Not a valid time");
});

test("can correctly convert last valid time 23:59", function () {
assert.equal(formatAs12HourClock("23:59"), "11:59 pm");
});

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

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

test("can correctly reject non-time strings", function () {
assert.equal(formatAs12HourClock("Hello"), "Not a valid time");
});

test("can correctly reject times with text in minute section", function () {
assert.equal(formatAs12HourClock("15:ab"), "Not a valid time");
});

test("can correctly reject only 2 digit number", function () {
assert.equal(formatAs12HourClock("12"), "Not a valid time");
});

test("can correctly reject time with text after", function () {
assert.equal(formatAs12HourClock("12:30abc"), "Not a valid time");
});

test("can correctly convert time using . as time separator", function () {
assert.equal(formatAs12HourClock("01.30"), "01:30 am");
});

test("can correctly convert time after 12 using . as time separator", function () {
assert.equal(formatAs12HourClock("15.30"), "03:30 pm");
});

test("can correctly convert time after using one digit for hour", function () {
assert.equal(formatAs12HourClock("3:30"), "03:30 am");
});

test("can correctly convert time after using one digit for hour and . for time separator", function () {
assert.equal(formatAs12HourClock("3.30"), "03:30 am");
});

Comment on lines +81 to +84

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is the same test as in line 81

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 have removed the accidental extra test

test("can correctly reject time using both . and :", function () {
assert.equal(formatAs12HourClock("12.30:45"), "Not a valid time");
});

test("can correctly reject time using more than one :", function () {
assert.equal(formatAs12HourClock("23::00"), "Not a valid time");
});

test("can correctly reject time using more than one .", function () {
assert.equal(formatAs12HourClock("23..00"), "Not a valid time");
});

test("can correctly reject time with no minutes after : ", function () {
assert.equal(formatAs12HourClock("23:"), "Not a valid time");
});

test("can correctly reject time with no minutes after . ", function () {
assert.equal(formatAs12HourClock("23."), "Not a valid time");
});

test("can correctly reject time with no hour before : ", function () {
assert.equal(formatAs12HourClock(":30"), "Not a valid time");
});

test("can correctly reject time with no hour before. ", function () {
assert.equal(formatAs12HourClock(".30"), "Not a valid time");
});

test("can correctly reject time with just . ", function () {
assert.equal(formatAs12HourClock("."), "Not a valid time");
});

test("can correctly reject time with negative minutes", function () {
assert.equal(formatAs12HourClock("15:-30"), "Not a valid time");
});

test("can correctly reject time with negative hours", function () {
assert.equal(formatAs12HourClock("-15:30"), "Not a valid time");
});

test("can correctly reject time with space character for hours", function () {
assert.equal(formatAs12HourClock(" :30"), "Not a valid time");
});

test("can correctly reject time with whitespace space characters after time", function () {
assert.equal(formatAs12HourClock("12:30 "), "Not a valid time");
});

test("can correctly reject time with newline characters after time", function () {
assert.equal(formatAs12HourClock("12:30\n"), "Not a valid time");
});
Loading