Repository navigation
London | 26-ITP-Sep | Sakiya Mayow | Sprint 1 | Structuring and Testing Data - #1639
zakiaao-tech wants to merge 8 commits into
Conversation
cjyuan
left a comment
There was a problem hiding this comment.
Your test misses several cases.
You should update your test first before revising your function implementation.
abdishakoor-dev
left a comment
There was a problem hiding this comment.
Writing the tests first and then adding minutes to the function, as your last two commits show, was the right order. 23:59 and 12:01 both come out right now.
There's one bug left in the function, and a few tests to add that will show it to you. Here are the steps:
- Add a test for each of these times:
"11:59","00:00","00:34","12:00". Write each test in the same shape as yours. The first"..."is the time going in. The second"..."is what your function should give back, as a 12-hour clock would show it. Decide that yourself before you run anything, for example: what does a 12-hour clock show at00:34?test("describe the time you are checking", function () { assert.equal(formatAs12HourClock("..."), "..."); });
- Run the tests. Right click the
format-clock-edge-casesfolder in VS Code, choose Open in Integrated Terminal, and runnode --test. The11:59test should fail and show you what your function really returns:11:59: 59 am. That's good. It means the test has found a bug, and every time from10:00to11:59has it. - Fix the bug. It's on
timeConverter.jsline 24 (see my comment there). Runnode --testagain until every test passes. - Sort out the four tests on lines 13 to 27. See my comment on line 13.
When all of that is pushed and every test passes, it should be ready to mark Complete. Add the Needs Review label again once you've pushed.
| return `0${hours}:${minutes} am`; | ||
| } | ||
|
|
||
| return `${time}: ${minutes} am`; |
There was a problem hiding this comment.
time holds the whole input, for example "10:30". So ${time} already includes the minutes, and : ${minutes} then adds them a second time, with a space in front. That's how you get 10:30: 30 am.
For 10:00 to 11:59 the hours already have two digits, so nothing needs changing about them. What does ${time} on its own give you, and is that all you need before am?
| assert.equal(formatAs12HourClock("23:59"), "11:59 pm"); | ||
| }); | ||
|
|
||
| test("converts 12:01 to 12:01 ", function () { |
There was a problem hiding this comment.
Bugs often hide at the exact point where something changes. 12:01 is the first minute after noon, so the last minute before noon, 11:59, is just as important. Your function gets 11:59 wrong at the moment, and a test for it would have shown you. Midnight (00:00) and noon (12:00) are the other two points where the clock changes, so they need a test each too.
| assert.equal(formatAs12HourClock("08:00"), "08:00 am"); | ||
| }); | ||
|
|
||
| test("correctly convert time after 12:00 ", function () { |
There was a problem hiding this comment.
These four tests all have the same name, "correctly convert time after 12:00". If one of them failed, the output wouldn't tell you which time broke. They also all check a whole hour between 2pm and 9pm, so they all test the same thing.
Keep one of them, and give it a name that says the time it checks, for example "converts 14:00 to 02:00 pm". Delete the other three. Your new tests from step 1 of my overall comment test far more.
cjyuan
left a comment
There was a problem hiding this comment.
Could you also respond to all the reviewers' comments in addition to making change?
Suggestion: Use AI to explore "how to respond to comments in a PR".
| if (time < 10) { | ||
| return `0${time}:${minutes} pm`; | ||
| } | ||
|
|
||
| return `${hours - 12}:${minutes} pm`; |
There was a problem hiding this comment.
Could you try simplifying the code on lines 16-20 into one line of code by taking advantage of a String's built-in method (function) ?
| test("converts 23:59 to 11:59 ", function () { | ||
| assert.equal(formatAs12HourClock("23:59"), "11:59 pm"); | ||
| }); | ||
|
|
||
| test("converts 12:01 to 12:01 ", function () { | ||
| assert.equal(formatAs12HourClock("12:01"), "12:01 pm"); | ||
| }); | ||
|
|
||
| test("converts 23:59 to 11:59 ", function () { | ||
| assert.equal(formatAs12HourClock("23:59"), "11:59 pm"); | ||
| }); |
There was a problem hiding this comment.
Thank you for the feedback. I've made changes, added the requested tests, and run node --test to check result.
| function formatAs12HourClock(time) { | ||
|
|
||
| const hours = Number(time.slice(0, 2)); | ||
| const minutes = time.slice(3, 5); |
There was a problem hiding this comment.
Note: The .slice() method supports negative indices, which count positions from the end of the string.
For example, str.slice(-3) returns the substring containing last three characters from str.
|
|
||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| const time = hours - 12; |
There was a problem hiding this comment.
time is also the name of the parameter.
Could you explain why using the same name for a local variable and a parameter is considered bad practice?
There was a problem hiding this comment.
Time is already the parameter in my function, declaring it again in the same scope can cause naming conflict.
|
It seems some of the inline comments were not yet addressed. Could you address all the inline comments in addition to making changes? Suggestion: Use AI to explore "How to respond to inline comments in a PR" |
Learners, PR Template
Self checklist
Task code
CYF-1197
Changelist
In this task I added an if statement because when testing the code I noticed that for any hours < 10 is a single digit number but we need double digits and added 0 where it was supposed to go. I then tested a few edge cases and fixed the function according to the mistakes and failed tests.