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
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
13
13
// At first I thought the error would be that last4Digits is not defined, but I learned that the error is actually because the slice method is being called on a number, which doesn't have that method.
14
14
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
15
-
constlast4Digits=cardNumber.toString().slice(-4);// first option
16
-
constlast4Digits=String(cardNumber).slice(-4);// second option
17
-
constlast4Digits=`${cardNumber}`.slice(-4);// third option
15
+
constlast4Digits=cardNumber.toString().slice(-4);// as slice is a string method, we need to convert the number to a string so that we can use the slice method on it. This can be done by using the toString() method or by using String(cardNumber) or by using template literals as shown in the code snippet
/* movieLength % 60 represents the remaining seconds when the movie length of 8784 is divided by 60, thus giving the leftover seconds that don't fit into a full minute.
23
-
*/
21
+
*/
24
22
25
23
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
26
24
/* The expression subtracts the leftover seconds from the movie length and divides it by 60 to give the full minutes. Here's a simple formula to understand the logic, movie length = full minutes + leftover seconds
27
-
*/
25
+
*/
28
26
29
27
// e) What do you think the variable result represents? Can you think of a better name for this variable?
// The variable result outputs the time in a formatted time string of hours:minutes:seconds
29
+
// It could be more clear to give it a better name like duration or movieDuration.
32
30
33
31
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
34
-
// I think it works for any positive number of seconds.
32
+
console.log(clockFormatter(0));
33
+
console.log(clockFormatter(69));
34
+
console.log(clockFormatter(15000));
35
+
console.log(clockFormatter(-10));
36
+
console.log(clockFormatter(20.4));
37
+
// yes, it worked for all values of movieLength, including 0, negative numbers, and decimal numbers. The code will work for any number, but the output may not be meaningful for negative or decimal values.
0 commit comments