@@ -14,21 +14,37 @@ function formatTimeDisplay(seconds) {
1414// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
1515// to help you answer these questions
1616
17- // Questions
17+ function pad ( num ) {
18+ return num . toString ( ) . padStart ( 2 , "0" ) ;
19+ }
1820
19- // a) When formatTimeDisplay is called how many times will pad be called?
20- // =============> write your answer here
21+ function formatTimeDisplay ( seconds ) {
22+ const remainingSeconds = seconds % 60 ;
23+ const totalMinutes = ( seconds - remainingSeconds ) / 60 ;
24+ const remainingMinutes = totalMinutes % 60 ;
25+ const totalHours = ( totalMinutes - remainingMinutes ) / 60 ;
26+
27+ return `${ pad ( totalHours ) } :${ pad ( remainingMinutes ) } :${ pad ( remainingSeconds ) } ` ;
28+ }
2129
22- // Call formatTimeDisplay with an input of 61, now answer the following:
30+ // Example call
31+ console . log ( formatTimeDisplay ( 61 ) ) ; // Output: "00:01:01"
32+
33+ // Answers to questions for input 61
34+
35+ // a) When formatTimeDisplay is called how many times will pad be called?
36+ console . log ( "a) Number of times pad is called: 3" ) ;
2337
2438// b) What is the value assigned to num when pad is called for the first time?
25- // =============> write your answer here
39+ console . log ( "b) Value assigned to num in first call: 0" ) ;
2640
27- // c) What is the return value of pad is called for the first time?
28- // =============> write your answer here
41+ // c) What is the return value of pad when called for the first time?
42+ console . log ( "c) Return value of pad in first call: '00'" ) ;
2943
30- // 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
44+ // d) What is the value assigned to num when pad is called for the last time? Explain your answer
45+ console . log ( "d) Value assigned to num in last call: 1 (remaining seconds)" ) ;
3246
33- // 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
47+ // e) What is the return value assigned to num when pad is called for the last time? Explain your answer
48+ console . log (
49+ "e) Return value of pad in last call: '01' (1 converted to 2 characters with leading 0)"
50+ ) ;
0 commit comments