You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sprint-1/3-mandatory-interpret/2-time-format.js
+36Lines changed: 36 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -24,4 +24,40 @@ console.log(result);
24
24
25
25
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
26
26
27
+
/*
27
28
29
+
a)variables declarations
30
+
31
+
1. There are 6 variable declarations
32
+
33
+
b) Function calls
34
+
35
+
There is only 1 function call
36
+
37
+
console.log(result);
38
+
39
+
c)Using documentation, explain what the expression movieLength % 60 represents
40
+
41
+
According to the JavaScript documentation, the remainder (%) operator returns the remainder left over after one number is divided by another.
42
+
43
+
So movieLength % 60 means Divide movieLength by 60 and return the remainder.
44
+
45
+
d) Interpret line 4. What does the expression assigned to totalMinutes mean?
46
+
47
+
It means remove the leftover seconds from the total movie length, then divide by 60 to calculate the total number of whole minutes.
48
+
49
+
50
+
e) What do you think the variable result represents? Can you think of a better name?
51
+
52
+
It stores the movie length formatted as hours:minutes:seconds
53
+
54
+
A better variable name could be formattedTime
55
+
56
+
57
+
f) Try experimenting with different values of movieLength.
58
+
59
+
It works correctly for positive whole numbers (integers) representing seconds. However, there are some cases where it doesn't produce ideal results like:
It means If this string is shorter than length, add the character to the beginning until it reaches that length. Our value already has 3 characters so nothing changes.
47
+
48
+
Step 4:
49
+
50
+
const pounds = paddedPenceNumberString.substring(
51
+
0,
52
+
paddedPenceNumberString.length - 2);
53
+
54
+
a) paddedPenceNumberString.length returns 3 then 3 - 2 is 1.
55
+
56
+
Why? because the last 2 digits are always the pence and everything before them is the pounds.
57
+
58
+
Step 5:
59
+
60
+
const pence = paddedPenceNumberString
61
+
.substring(paddedPenceNumberString.length - 2)
62
+
.padEnd(2, "0");
63
+
64
+
a) substring(length - 2) the length is 3 so substring(1) means Start at index 1 and keep going to the end.
65
+
66
+
b) .padEnd(2, "0") which means if the string is shorter than 2 characters, add "0" to the end.
67
+
68
+
69
+
Step 6:
70
+
71
+
console.log(`£${pounds}.${pence}`);
72
+
73
+
Inside it is a template literal, which uses backticks (`): and `£${pounds}.${pence}` means insert the value of this variable here.
74
+
75
+
If pounds = "3" pence = "99" then javascript prints 3.99
0 commit comments