Repository navigation
Cape Town | 26-ITP-Sept | Leigh Ross | Sprint 1 | Exhaustively test and fix formatAs12HourClock #1646
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?
Cape Town | 26-ITP-Sept | Leigh Ross | Sprint 1 | Exhaustively test and fix formatAs12HourClock #1646
Changes from all commits
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,25 @@ | ||
| function formatAs12HourClock(time) { | ||
|
|
||
| const hours = Number(time.slice(0, 2)); | ||
| const minutes = time.slice(-2); | ||
| let hours12; | ||
| let timePeriod; | ||
|
|
||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| if (hours === 0) { | ||
| hours12 = "12"; | ||
| timePeriod = "am"; | ||
| } | ||
| else if (hours === 12) { | ||
| hours12 = "12"; | ||
| timePeriod = "pm"; | ||
| } | ||
| else if (hours < 12) { | ||
| hours12 = String(hours).padStart(2, "0"); | ||
| timePeriod = "am"; | ||
| } | ||
| return `${time} am`; | ||
| else if (hours > 12) { | ||
| hours12 = String(hours - 12).padStart(2, "0"); | ||
| timePeriod = "pm" | ||
| } | ||
| return `${hours12}:${minutes} ${timePeriod}` | ||
| } | ||
|
|
||
| export {formatAs12HourClock}; | ||
| export {formatAs12HourClock}; | ||
|
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. Part of the challenge in this exercise is to design a comprehensive test suite that can detect potential errors not only in your own function implementation but also in future modifications made by others. What additional test data should be included to make the test suite more comprehensive?
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. I have added more test cases to really catch any further bugs. It was a bit difficult so, I used a bit of help from AI especially for that last 24 hour sweep test.
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 a thorough test. Did you pick up any thought process or logic from the AI for deriving the test data? This way, you'd have a better idea of what to test or check when implementing similar functions in the future. |
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.
Code works.
Could you use AI to explore alternatives to simplify the code on lines 4-22 into 3-4 lines of code?
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.
This is what AI gave me:
Option 1: Ternary + modulo (3 lines)
const hours12 = hours % 12 || 12;
const timePeriod = hours < 12 ? "am" : "pm";
return
${String(hours12).padStart(2, "0")}:${minutes} ${timePeriod};Option 2: Single expression (1 line)
return
${String(hours % 12 || 12).padStart(2, "0")}:${minutes} ${hours < 12 ? "am" : "pm"};Option 3: With toLocaleTimeString (fully different approach)
const [h, m] = time.split(":").map(Number);
return new Date(2000, 0, 1, h, m).toLocaleTimeString("en-US", {
hour: "2-digit", minute: "2-digit", hour12: true
}).toLowerCase();
Out of those 3 options, I like option 1 the best:
Would you like me to change my code so that option 1 is implemented correctly?
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.
The code submitted to a PR should be one's best work.
So I think it is a good practice to apply what you think is an improved solution.
Also, by implementing them, you could strengthen you understanding.
I agree option 1 is better than then other two.
What do you mean? Why would the change involve any global variables?