@@ -15,24 +15,52 @@ function formatTimeDisplay(seconds) {
1515 return `${ pad ( totalHours ) } :${ pad ( remainingMinutes ) } :${ pad ( remainingSeconds ) } ` ;
1616}
1717
18+ console . log ( formatTimeDisplay ( 61 ) ) ; // Should print "00:01:01"
19+
1820// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
1921// to help you answer these questions
2022
2123// Questions
2224
2325// a) When formatTimeDisplay is called how many times will pad be called?
2426// =============> write your answer here
27+ // The pad function is called 3 times within formatTimeDisplay: once
28+ // for totalHours, once for remainingMinutes, and once for
29+ // remainingSeconds. Therefore, pad will be called 3 times.
30+
2531
2632// Call formatTimeDisplay with an input of 61, now answer the following:
2733
2834// b) What is the value assigned to num when pad is called for the first time?
2935// =============> write your answer here
36+ // 0 (totalHours) is assigned to num because 61 seconds is less than
37+ // 3600 seconds (1 hour), so totalHours is calculated as 0.
3038
3139// c) What is the return value of pad is called for the first time?
3240// =============> write your answer here
41+ // "00" is the return value of pad because num is 0 because totalHours
42+ // is 0, and the while loop in pad will add a "0" to the left of
43+ // numString until its length is at least 2. Since numString starts as
44+ // "0", it will become "00" after one iteration of the loop, and then it
45+ // will be returned as the final result of the first call to pad. Also,
46+ // "0".length is 1, which is less than 2, so the loop will execute once,
47+ // prepends "0" and makes "00". Now "00".length is 2, which is not less
48+ // than 2, so the loop will stop and "00" will be returned.
3349
3450// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
3551// =============> write your answer here
52+ // For seconds = 61, the last call to pad is for remainingSeconds.
53+ // AS calculated in (b), remainingSeconds is 1. So the value assigned
54+ // to num when pad is called for the last time is 1. This is because
55+ // remainingSeconds is calculated as seconds % 60, which gives the
56+ // remainder of 61 divided by 60, resulting in 1. Therefore, num is
57+ // assigned the value of 1 during the last call to pad in this program.
58+
3659
3760// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
3861// =============> write your answer here
62+ // As calculated in (d), num is 1. The pad function converst num to
63+ // a string ("1") and then checks its length. Since the length is 1,
64+ // it prepends "0" to make it to "01". So, the return value of pad
65+ // when called for the last time is "01". This is because the while loop
66+ // in pad will execute once.
0 commit comments