Skip to content

Commit f6b823d

Browse files
committed
time display formatting explained
1 parent 97e6d94 commit f6b823d

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed
Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
function pad(num) {
1+
function pad(0) {
22
return num.toString().padStart(2, "0");
33
}
44

5-
function formatTimeDisplay(seconds) {
5+
function formatTimeDisplay(61) {
66
const remainingSeconds = seconds % 60;
77
const totalMinutes = (seconds - remainingSeconds) / 60;
88
const remainingMinutes = totalMinutes % 60;
@@ -17,18 +17,23 @@ function formatTimeDisplay(seconds) {
1717
// Questions
1818

1919
// a) When formatTimeDisplay is called how many times will pad be called?
20-
// =============> write your answer here
20+
// 3 times: the return statement contains 3 pad calls within the formatTimeDisplay function.
2121

2222
// Call formatTimeDisplay with an input of 61, now answer the following:
2323

2424
// b) What is the value assigned to num when pad is called for the first time?
25-
// =============> write your answer here
25+
// num = 0 as when the input is 61 seconds, remainingSeconds = 1, totalMinutes = 1, remainingMinutes = 1, totalHours = 0.
26+
// totalHours = (totalMinutes - remainingMinutes) / 60 = (1 - 1) / 60 = 0 / 60 = 0 (dividing once into minutes and once into hours).
2627

27-
// c) What is the return value of pad is called for the first time?
28-
// =============> write your answer here
28+
// c) What is the return value of pad when called for the first time?
29+
// "00" as num is 0, and padStart(2, "0") adds a leading zero to make it two digits.
2930

3031
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
31-
// =============> write your answer here
32+
// The last time pad is called, num is assigned the value of remainingSeconds, which is 1.
33+
// Go to line const remainingSeconds = seconds % 60; and use input of seconds = 61 to calculate remainder of 61/60 = 1
3234

3335
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
34-
// =============> write your answer here
36+
// The last time pad is called, it uses remainingSeconds which is 1
37+
// The call is pad(1) which converts to a string and adds a leading 0 if there's less than 2 characters
38+
// So, the return value is "01".
39+
// The final formatted time string for an input of 61 seconds is "00:01:01".

0 commit comments

Comments
 (0)