Skip to content

Glasgow | 26-ITP-May | Niangh Ciang | Sprint 2 | Coursework#1367

Open
Niangh-Ciang wants to merge 13 commits into
CodeYourFuture:mainfrom
Niangh-Ciang:coursework/sprint-2
Open

Glasgow | 26-ITP-May | Niangh Ciang | Sprint 2 | Coursework#1367
Niangh-Ciang wants to merge 13 commits into
CodeYourFuture:mainfrom
Niangh-Ciang:coursework/sprint-2

Conversation

@Niangh-Ciang

Copy link
Copy Markdown

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Changelist

Completed the courwork exercises in Sprint 2.

@Niangh-Ciang Niangh-Ciang added 📅 Sprint 2 Assigned during Sprint 2 of this module Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Module-Structuring-And-Testing-Data The name of the module. labels Jun 20, 2026
Comment thread Sprint-2/2-mandatory-debug/2.js
Comment thread Sprint-2/3-mandatory-implement/1-bmi.js
Comment thread Sprint-2/3-mandatory-implement/2-cases.js
Comment on lines 5 to 18
function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
}
if (hours === 12) {
return `${hours}:00 pm`;
}
if (hours === 0) {
const minutes = time.slice(3, 5);
return `12:${minutes} am`;
}
return `${time} am`;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First, without running the code, predict the output from the following function calls.
Then, check if your function is returning the value you expected?

console.log( formatAs12HourClock("12:35") );
console.log( formatAs12HourClock("01:01") );
console.log( formatAs12HourClock("13:01") );

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I tested "12:35" and "13:01", the minutes were wrong because I originally put const minutes = time.slice(3, 5) inside the hours === 0 block, so only the midnight case kept the minutes.
For the other cases (hours > 12 and hours === 12), the function always returned :00.
I fixed it by moving const minutes to the top of the function and using ${minutes} in every return statement. Now all the tests pass.

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Jun 23, 2026
@Niangh-Ciang Niangh-Ciang added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Jun 23, 2026

@cjyuan cjyuan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good. Your responses were clear and to the point. Excellent job!


function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
const minutes = time.slice(3, 5);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.slice() supports negative index. So we could also extract the last two characters as time.slice(-2).

Comment on lines +57 to +68
const currentOutput7 = formatAs12HourClock("01:01");
const targetOutput7 = "01:01 am";
console.assert(
currentOutput7 === targetOutput7,
`current output: ${currentOutput7}, target output: ${targetOutput7}`
);
const currentOutput8 = formatAs12HourClock("13:01");
const targetOutput8 = "1:01 pm";
console.assert(
currentOutput8 === targetOutput8,
`current output: ${currentOutput8}, target output: ${targetOutput8}`
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If "01:01" is converted to "01:01 am", it is probably reasonable for the caller to expect "13:01" to be converted to "01:01 pm".

When the returned values are not formatted consistently, it may result in unintended side-effect. For examples,

  1. When the strings are displayed, "01:00 pm" and "1:00 pm" would not align as nicely.
01:00 am
1:00 pm
12:00 am
01:00 pm
  1. When the formatted strings are compared in the program, "1:00 pm" < "11:00 pm" and 01:00 am" < "11:00 am" produce different results.

Consistency is important so the caller can be certain what to expect from a function.

@cjyuan cjyuan added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Jun 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed. Module-Structuring-And-Testing-Data The name of the module. 📅 Sprint 2 Assigned during Sprint 2 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants