Skip to content

Commit d46a3dd

Browse files
authored
Refactor time formatting and add console logs
1 parent 4366cc4 commit d46a3dd

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
1-
function pad(num) {
2-
return num.toString().padStart(2, "0");
1+
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
32
}
43

5-
function formatTimeDisplay(seconds) {
6-
const remainingSeconds = seconds % 60;
7-
const totalMinutes = (seconds - remainingSeconds) / 60;
8-
const remainingMinutes = totalMinutes % 60;
9-
const totalHours = (totalMinutes - remainingMinutes) / 60;
4+
console.log(formatTimeDisplay(61));
5+
console.log(formatTimeDisplay(6671));
6+
console.log(formatTimeDisplay(832));
107

11-
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
12-
}
138

149
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
1510
// to help you answer these questions
1611

1712
// Questions
1813

1914
// a) When formatTimeDisplay is called how many times will pad be called?
20-
// =============> write your answer here
15+
// =============> write your answer here: pad will be called 3 times.
2116

2217
// Call formatTimeDisplay with an input of 61, now answer the following:
2318

2419
// b) What is the value assigned to num when pad is called for the first time?
25-
// =============> write your answer here
20+
// =============> write your answer here: The value is 0.
2621

2722
// c) What is the return value of pad is called for the first time?
28-
// =============> write your answer here
23+
// =============> write your answer here: The return value is '00'.
2924

3025
// 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
26+
// =============> write your answer here: The value is 1 because 61 % 60 leaves 1 second remaining.
3227

3328
// 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
29+
// =============> write your answer here: The return value is '01' because return num.toString().padstart(2, '0') formats 1 into '01'.

0 commit comments

Comments
 (0)