@@ -11,24 +11,38 @@ function formatTimeDisplay(seconds) {
1111 return `${ pad ( totalHours ) } :${ pad ( remainingMinutes ) } :${ pad ( remainingSeconds ) } ` ;
1212}
1313
14+ console . log ( formatTimeDisplay ( 61 ) ) ;
1415// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
1516// to help you answer these questions
1617
1718// Questions
1819
1920// a) When formatTimeDisplay is called how many times will pad be called?
20- // =============> write your answer here
21+ // =============> pad will be called 3 times, once for pad(totalHours,
22+ // once for (pad)remainingMinutes and once for (pad)remainingSeconds
2123
2224// Call formatTimeDisplay with an input of 61, now answer the following:
2325
2426// b) What is the value assigned to num when pad is called for the first time?
25- // =============> write your answer here
27+ // =============> The first call to pad is pad(totalHours).
28+ totalHours is calculated as totalMinutes // 60.
29+ For the input 61 , seconds is 61 , remainingSeconds is 1 , and totalMinutes is 1.
30+ So , totalHours is 1 // 60 = 0.
31+ Therefore , num is assigned the value 0 when pad is called for the first time .
32+
2633
2734// c) What is the return value of pad is called for the first time?
28- // =============> write your answer here
35+ // =============> The first call to pad is pad(totalHours).
36+ totalHours is calculated as totalMinutes // 60.
37+ For the input 61 , totalMinutes is ( 61 - 1 ) // 60 = 1.
38+ So , totalHours is 1 // 60 = 0.
39+ pad ( 0 ) returns the string representation of 0 , padded with zeros to a minimum length of 2 , which is "00" .
2940
3041// 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
42+ // =============> We look at the last call to pad, which is pad(remaining_seconds).
43+ remaining_seconds is calculated as seconds % 60.
44+ For the input 61 , seconds % 60 equals 1.
45+ So , pad ( remaining_seconds ) is equivalent to pad ( 1 ) . Therefore , the value assigned to num in the last call to pad is 1.
3246
3347// 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
48+ // =============> For the input 61, remaining_seconds will be 1. So, pad(1) will return "01". Therefore, the value assigned to num is 1.
0 commit comments