diff --git a/format-clock-edge-cases/README.md b/format-clock-edge-cases/README.md new file mode 100644 index 0000000000..8a43a9e964 --- /dev/null +++ b/format-clock-edge-cases/README.md @@ -0,0 +1,15 @@ +In your prep, you looked this `formatAs12HourClock` function: + +```js +function formatAs12HourClock(time) { + + const hours = Number(time.slice(0, 2)); + + if (hours > 12) { + return `${hours - 12}:00 pm`; + } + return `${time} am`; +} +``` + +It still has bugs. Think of as many edge-cases as you can with this code. Write tests for all of them, and fix this code so that it works correctly for all valid inputs. You don't need to worry about invalid inputs (e.g. `"25:00"`). diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js new file mode 100644 index 0000000000..310c07bd9c --- /dev/null +++ b/format-clock-edge-cases/timeConverter.js @@ -0,0 +1,11 @@ +function formatAs12HourClock(time) { + + const hours = Number(time.slice(0, 2)); + + if (hours > 12) { + return `${hours - 12}:00 pm`; + } + return `${time} am`; +} + +export {formatAs12HourClock}; diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js new file mode 100644 index 0000000000..88f2af0948 --- /dev/null +++ b/format-clock-edge-cases/timeConverter.test.js @@ -0,0 +1,11 @@ +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 correctly convert morning time", function() { + assert.equal(formatAs12HourClock("08:00"), "08:00 am"); +});