11function pad ( num ) {
2- let numString = num . toString ( ) ;
3-
4- while ( numString . length < 2 ) {
5- numString = "0" + numString ;
6- }
7- return numString ;
2+ return num . toString ( ) . padStart ( 2 , "0" ) ;
83}
9-
10-
114function formatTimeDisplay ( seconds ) {
125 const remainingSeconds = seconds % 60 ;
136 const totalMinutes = ( seconds - remainingSeconds ) / 60 ;
@@ -16,25 +9,22 @@ function formatTimeDisplay(seconds) {
169
1710 return `${ pad ( totalHours ) } :${ pad ( remainingMinutes ) } :${ pad ( remainingSeconds ) } ` ;
1811}
19- console . log ( formatTimeDisplay ( 61 ) )
20-
21-
22-
12+ console . log ( formatTimeDisplay ( 61 ) ) ;
2313
2414// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
2515// to help you answer these questions
2616
2717// Questions
2818
2919// a) When formatTimeDisplay is called how many times will pad be called?
30- // =============> write your answer here
20+ // =============> write your answer here
3121// Answer below:
3222// Pad runs once for "totalHours", once "remainingMinutes" and once for "remainingSeconds" in total pad is called 3 times.
3323
3424// Call formatTimeDisplay with an input of 61, now answer the following:
3525
3626// b) What is the value assigned to num when pad is called for the first time?
37- // =============> write your answer here
27+ // =============> write your answer here
3828// Answer below:
3929// The first pad is pad(totalHours) and the value is 0
4030
@@ -43,11 +33,11 @@ console.log(formatTimeDisplay(61))
4333// The value is 0 plus the "0" added to it to make the value 00
4434
4535// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
46- // =============> write your answer here
36+ // =============> write your answer here
4737// Answer below:
4838// The last call is pad(remainingSeconds) and the value is 1
4939
5040// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
51- // =============> write your answer here
41+ // =============> write your answer here
5242// Answer below:
5343// the return value initially is 1 and "0" was added to it in front to make the value 01
0 commit comments