1- function pad ( num ) {
2- let numString = num . toString ( ) ;
3- while ( numString . length < 2 ) {
1+ function pad ( num ) { //1)num=0, 2)num=0 3) num=1
2+ let numString = num . toString ( ) ; // (last time) numString = "1"
3+ while ( numString . length < 2 ) { //"01" it will return 01
44 numString = "0" + numString ;
55 }
66 return numString ;
77}
88
9- function formatTimeDisplay ( seconds ) {
10- const remainingSeconds = seconds % 60 ;
11- const totalMinutes = ( seconds - remainingSeconds ) / 60 ;
12- const remainingMinutes = totalMinutes % 60 ;
13- const totalHours = ( totalMinutes - remainingMinutes ) / 60 ;
9+ function formatTimeDisplay ( seconds ) {
10+ const remainingSeconds = seconds % 60 ; //61%60 = 1
11+ const totalMinutes = ( seconds - remainingSeconds ) / 60 ; //1-1=0
12+ const remainingMinutes = totalMinutes % 60 ; //0
13+ const totalHours = ( totalMinutes - remainingMinutes ) / 60 ; // 0
1414
15- return `${ pad ( totalHours ) } :${ pad ( remainingMinutes ) } :${ pad ( remainingSeconds ) } ` ;
15+ return `${ pad ( totalHours ) } :${ pad ( remainingMinutes ) } :${ pad ( remainingSeconds ) } ` ;
16+ //
1617}
18+ console . log ( formatTimeDisplay ( 61 ) ) ;
1719
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
@@ -22,17 +24,21 @@ function formatTimeDisplay(seconds) {
2224
2325// a) When formatTimeDisplay is called how many times will pad be called?
2426// =============> write your answer here
25-
27+ //3 in console.log
2628// Call formatTimeDisplay with an input of 61, now answer the following:
2729
2830// b) What is the value assigned to num when pad is called for the first time?
2931// =============> write your answer here
30-
32+ //0
3133// c) What is the return value of pad is called for the first time?
3234// =============> write your answer here
33-
35+ //"00"
3436// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
3537// =============> write your answer here
36-
38+ ////1)num=0, 2)num=0 3) num=1, when num is called for last time that is when pad(remainingSeconds),
39+ //here remainingseconds value = 1 so num is assigned with value of 1
3740// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
3841// =============> write your answer here
42+ //when pad called for the last time, the value of num is 1
43+ //it will convert into string by toString method "1" as its less than 2, it satisfies the while loop condition and,
44+ // will concatenate with "0" the final value of variable numString would be "01", and will return "01".
0 commit comments