1+ let timesToBeCalled = 0 ;
2+
13function pad ( num ) {
4+ timesToBeCalled += 1 ;
5+ console . log (
6+ ` Pad has been called ${ timesToBeCalled } times and num is ${ num } `
7+ ) ;
8+ console . log ( ` The return value will be ${ num . toString ( ) . padStart ( 2 , "0" ) } ` ) ;
29 return num . toString ( ) . padStart ( 2 , "0" ) ;
310}
411
@@ -11,24 +18,36 @@ function formatTimeDisplay(seconds) {
1118 return `${ pad ( totalHours ) } :${ pad ( remainingMinutes ) } :${ pad ( remainingSeconds ) } ` ;
1219}
1320
21+ console . log ( formatTimeDisplay ( 61 ) ) ;
22+
1423// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
1524// to help you answer these questions
1625
1726// Questions
1827
1928// a) When formatTimeDisplay is called how many times will pad be called?
2029// =============> write your answer here
30+ // The pad function will be called 3 times when formatTimeDisplay is called.
31+ // 1st: for totalHours
32+ // 2nd: for remainingMinutes
33+ // 3rd: for remainingSeconds
2134
2235// Call formatTimeDisplay with an input of 61, now answer the following:
2336
2437// b) What is the value assigned to num when pad is called for the first time?
2538// =============> write your answer here
39+ // The value assigned to num is 0 when pad is called for the first time because 61 seconds is 0 hours, 1 minute, and 1 second.
2640
2741// c) What is the return value of pad is called for the first time?
2842// =============> write your answer here
43+ // The return value of pad is "00" in string format because 0 is padded to 2 digits with leading zeros.
2944
3045// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
3146// =============> write your answer here
47+ // The value assigned to num is 1 when pad is called for the last time.
48+ // It is because the remaining seconds is 1 second after taken 60 seconds for 1 minute.
3249
3350// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
3451// =============> write your answer here
52+ // The return value assigned to num is "01" in string format.
53+ // It is because 1 second is padded to 2 digits with a leading zero.
0 commit comments