Repository navigation
London | 26-ITP-Sep | Alan Mak | Sprint 1 | formatAs12HourClock Coursework #1641
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
ae73566
62033c7
bcf5419
8b179f7
f6b1388
9bb46ff
8255bd2
fee5fba
dcf5d9f
b350b54
c4cfbd3
7eb6e65
a08f8d8
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,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`; | ||
| } else if (hours < 1) { | ||
| return `12:${minutes} am`; | ||
| } | ||
| return `${time} am`; | ||
| return `0${hours}:${minutes} am`; | ||
| } | ||
|
|
||
| export {formatAs12HourClock}; | ||
| export { formatAs12HourClock }; | ||
| 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"); | ||
|
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. Your
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. Added additional test cases for last minute of morning, last minute of the day, first minute of the day, and first minute after noon.
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. All tests passed.
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. 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"); | ||
|
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. Bugs often hide at the exact point where something changes.
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. Added two test cases for edge scenarios: last minutes in each session of the day. Both tests passed.
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. 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 () { | ||
|
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. Lines 29 and 33 both call
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. Changed the hour in second test on line 33 - to test minutes input shows correctly. Test passed.
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.
|
||
| 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"); | ||
| }); | ||
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.
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?
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.
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
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.
Agreed, the two branches give different answers now.