Repository navigation
London | 26-ITP-Sept | Hugh Mills | Sprint 1 | formatAs12HourClock #1636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e5c8086
1f0220a
f85f166
77d1ade
158eba2
cf1ca72
b56500c
2fc03a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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)); | ||
|
|
||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| if (hours === 12) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You've handled the At the moment Hint: add another check like the one on this line, but for When it works you should get:
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's it. |
||
| 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 }; | ||
| 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"); | ||
| }); |
There was a problem hiding this comment.
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:
minis turned into a number here, and then on line 14 it is turned back into a string and padded with a0. If you kept it as a string,time.slice(-2)already gives you"04"with the0in place. Then you wouldn't need.toString().padStart(...)for the minutes.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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")gives4, which is why you needed thepadStart. It works as it is, so leave it.