Repository navigation
London | 26-ITP-Sep | Alan Mak | Sprint 1 | formatAs12HourClock Coursework - #1641
AlanGit-debug2604 wants to merge 13 commits into
Conversation
…in 12-hour clock conversion
…hour === 12 for noon
…due characters morning, noon time, and shows minutes
abdishakoor-dev
left a comment
There was a problem hiding this comment.
Your function now handles all three bugs from the starter: minutes are kept after noon, 12:xx is pm, and 00:xx is 12 am. Testing the hours with one digit and two digits separately (lines 17 and 21) was a good way to look for edge cases.
Three things before I can mark this Complete:
timeConverter.test.jsline 10: the expected value of a starter test has changed. See my comment there..gitignoreis changed in this PR, but it isn't part of the task. See my comment there.- Two edge cases have no test yet. See my comment on
timeConverter.test.jsline 22.
On your question about switch: a plain switch (hours) compares hours against each case with ===, so it suits exact values like 12 but not ranges like hours > 12. There is a switch (true) pattern that allows ranges, but for conditions like yours most people find if / else if easier to read. I think having fewer branches would help readability more than changing the keyword. My comment on timeConverter.js line 10 is a place to start.
Add the Needs Review label again once you've pushed.
| 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"), "8:00 am"); |
There was a problem hiding this comment.
The starter expected "08:00 am" here, with the zero. The starter tests describe what the function must return, so they stay as written. Please put back "08:00 am" and run the tests. It will fail, and that's fine: it means timeConverter.js is what needs to change. Then check that line 18 uses the same format.
There was a problem hiding this comment.
Fixed two morning time test cases back to starter tests expected results e.g. "08:00am". See changes on line 10 and 18
There was a problem hiding this comment.
Also ran an initial test after correction, validated two morning time branches need to change.
| .vscode | ||
| **/.DS_Store No newline at end of file | ||
| **/.DS_Store | ||
| Prep/ No newline at end of file |
There was a problem hiding this comment.
This PR should only change files in format-clock-edge-cases/. To undo this change, run git checkout main -- .gitignore on your branch, then commit and push. If you want git to ignore your Prep/ folder only on your own computer, you can add it to .git/info/exclude instead. That file is never committed.
There was a problem hiding this comment.
Ran undo change, committed, and pushed.
Also added Prep to last line of .git/info/exclude
There was a problem hiding this comment.
Gone from the PR now. Thanks.
| } else if (hours === 12) { | ||
| return `12:${minutes} pm`; | ||
| } else if (hours > 9) { | ||
| return `${hours}:${minutes} am`; |
There was a problem hiding this comment.
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.
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.
Agreed, the two branches give different answers now.
| }); | ||
|
|
||
| test("can correctly convert dual characters hour morning time", function () { | ||
| assert.equal(formatAs12HourClock("11:00"), "11:00 am"); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Added two test cases for edge scenarios: last minutes in each session of the day.
Both tests passed.
There was a problem hiding this comment.
Those are the two. Good.
| }); | ||
|
|
||
| test("can correctly shows minutes input", function () { | ||
| assert.equal(formatAs12HourClock("16:59"), "4:59 pm"); |
There was a problem hiding this comment.
Optional, not needed for Complete: once morning times keep their zero ("08:00 am"), should 16:59 give "4:59 pm" or "04:59 pm"? Whichever you pick, it's worth being the same for am and pm.
There was a problem hiding this comment.
Keep converter and test align to stay result time format the same as 0X:00 for am and pm.
Validated and passed in test file.
There was a problem hiding this comment.
Consistent now. Good.
…than 21, to line up with expect time formatting in test file.
abdishakoor-dev
left a comment
There was a problem hiding this comment.
Everything from last round is fixed, and you now use the same zero-padded format for am and pm, so the output reads consistently across the day.
Two things left before I can mark it Complete:
timeConverter.test.jslines 29 and 33 are the same test. See my comment there.- A few boundaries still have no test: a time just after midnight, just after noon, and the first minute of the afternoon. See my comment on line 14. I missed these last time, sorry.
Add the Needs Review label again once you've pushed.
| assert.equal(formatAs12HourClock("12:00"), "12:00 pm"); | ||
| }); | ||
|
|
||
| test("can correctly convert to single character afternoon time", function () { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Changed the hour in second test on line 33 - to test minutes input shows correctly.
Test passed.
There was a problem hiding this comment.
13:30 tests something new. Good.
| }); | ||
|
|
||
| test("can correctly convert midnight time", function () { | ||
| assert.equal(formatAs12HourClock("00:00"), "12:00 am"); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Added additional test cases for last minute of morning, last minute of the day, first minute of the day, and first minute after noon.
There was a problem hiding this comment.
All three are there now. Good.
abdishakoor-dev
left a comment
There was a problem hiding this comment.
The three boundaries are tested now, and the duplicate is gone. Your tests cover every point where the clock changes, from 00:01 through to 23:59. Nothing left to do, marking this Complete. Well done.
Alan Mak, PR Template
Self checklist
Task code
CYF-1197
Changelist
Edit lines in timeConverter.js for edge cases within valid inputs. Edit lines in timeConverter.test.js for five additional test cases. All tests run successfully.
Questions
For the purpose of stretching this task, I am researching on time Converter logic other than using if and else if, which produce long chunk of code lines when condition expands. I am wondering the readability for reviewers using key word
switch?