Skip to content

Commit 5ee1594

Browse files
committed
time format function explanation
1 parent 629157a commit 5ee1594

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

Sprint-2/4-mandatory-interpret/time-format.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,36 @@ function pad(num) {
99
function formatTimeDisplay(seconds) {
1010
const remainingSeconds = seconds % 60;
1111
const totalMinutes = (seconds - remainingSeconds) / 60;
12+
1213
const remainingMinutes = totalMinutes % 60;
1314
const totalHours = (totalMinutes - remainingMinutes) / 60;
1415

1516
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1617
}
1718

19+
console.log(formatTimeDisplay(61));
20+
1821
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
1922
// to help you answer these questions
2023

2124
// Questions
2225

2326
// a) When formatTimeDisplay is called how many times will pad be called?
24-
// =============> write your answer here
27+
// =============>
28+
// the function formatTImeDisplay calls 3 times the pad function -pad(totalHours) pad(remainingMinutes) pad(remainingSeconds)
2529

2630
// Call formatTimeDisplay with an input of 61, now answer the following:
2731

2832
// b) What is the value assigned to num when pad is called for the first time?
29-
// =============> write your answer here
33+
// =============> the number 0 = which is the calculation of const totalHours = (totalMinutes - remainingMinutes) / 60;
3034

3135
// c) What is the return value of pad is called for the first time?
32-
// =============> write your answer here
36+
// =============> "00" = the function pad(-receives the number 0 or the value of totalHours) and converts this to a string =
37+
// than the while loop kicks in and runs while the conditions is true and exit once it returns as false
38+
// numString = "0" + numString; this line will add a a "0" to our new string if our condition is true - (numString.length < 2)
3339

3440
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
35-
// =============> write your answer here
41+
// =============> number 1 - the calculation of const remainingSeconds = seconds % 60;
3642

3743
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
38-
// =============> write your answer here
44+
// =============> "01" pad(1) - converts to a string "1" and adds the "0" + with the while loop to our newly created string

0 commit comments

Comments
 (0)