Skip to content

Commit f74cc73

Browse files
committed
sprint 3 ex4-5
1 parent c9730ff commit f74cc73

2 files changed

Lines changed: 59 additions & 5 deletions

File tree

‎Sprint-3/4-mandatory-interpret/time-format.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ function formatTimeDisplay(seconds) {
2121
// Questions
2222

2323
// a) When formatTimeDisplay is called how many times will pad be called?
24-
// =============> write your answer here
24+
// =============> write your answer here : 3 times, once for totalHours, once for remainingMinutes, once for remainingSeconds, since the template literal contains three separate pad(...) calls.
2525

2626
// Call formatTimeDisplay with an input of 61, now answer the following:
2727

2828
// b) What is the value assigned to num when pad is called for the first time?
29-
// =============> write your answer here
29+
// =============> write your answer here : totalHours is 0, num is 0 on the first call.
3030

3131
// c) What is the return value of pad when it is called for the first time?
32-
// =============> write your answer here
32+
// =============> write your answer here "00"
3333

3434
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
35-
// =============> write your answer here
35+
// =============> write your answer here : pad(remainingSeconds), and since remainingSeconds is 1, num is 1 on this final call.
3636

3737
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
38-
// =============> write your answer here
38+
// =============> write your answer here : "01"

‎Sprint-3/5-stretch-extend/format-time.js‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,57 @@ console.assert(
2323
currentOutput2 === targetOutput2,
2424
`current output: ${currentOutput2}, target output: ${targetOutput2}`
2525
);
26+
27+
28+
// Midnight
29+
const currentOutput3 = formatAs12HourClock("00:00");
30+
const targetOutput3 = "12:00 am";
31+
console.assert(
32+
currentOutput3 === targetOutput3,
33+
`current output: ${currentOutput3}, target output: ${targetOutput3}`
34+
);
35+
36+
// Noon
37+
const currentOutput4 = formatAs12HourClock("12:00");
38+
const targetOutput4 = "12:00 pm";
39+
console.assert(
40+
currentOutput4 === targetOutput4,
41+
`current output: ${currentOutput4}, target output: ${targetOutput4}`
42+
);
43+
44+
// PM time with non-zero minutes
45+
const currentOutput5 = formatAs12HourClock("13:45");
46+
const targetOutput5 = "01:45 pm";
47+
console.assert(
48+
currentOutput5 === targetOutput5,
49+
`current output: ${currentOutput5}, target output: ${targetOutput5}`
50+
);
51+
52+
// Just before midnight
53+
const currentOutput6 = formatAs12HourClock("23:59");
54+
const targetOutput6 = "11:59 pm";
55+
console.assert(
56+
currentOutput6 === targetOutput6,
57+
`current output: ${currentOutput6}, target output: ${targetOutput6}`
58+
);
59+
60+
61+
//FIXED VERSIONS
62+
63+
function formatAs12HourClock(time) {
64+
const [hoursString, minutes] = time.split(":");
65+
let hours = Number(hoursString);
66+
let period = "am";
67+
68+
if (hours === 0) {
69+
hours = 12;
70+
} else if (hours === 12) {
71+
period = "pm";
72+
} else if (hours > 12) {
73+
hours = hours - 12;
74+
period = "pm";
75+
}
76+
77+
const paddedHours = hours.toString().padStart(2, "0");
78+
return `${paddedHours}:${minutes} ${period}`;
79+
}

0 commit comments

Comments
 (0)