1- const movieLength = 8784 ; // length of movie in seconds
1+ const movieLength = true ; // length of movie in seconds
22
33const remainingSeconds = movieLength % 60 ;
44const totalMinutes = ( movieLength - remainingSeconds ) / 60 ;
@@ -13,13 +13,48 @@ console.log(result);
1313
1414// a) How many variable declarations are there in this program?
1515
16+ // There are 6 variable declarations in this program.
17+
1618// b) How many function calls are there?
1719
20+ // There is 1 function call in this program.
21+
1822// c) Using documentation, explain what the expression movieLength % 60 represents
1923// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
2024
21- // d) Interpret line 4, what does the expression assigned to totalMinutes mean?
25+ // The expression movieLength % 60 is using the modulus operator (%) to calculate the remainder when movieLength is divided by 60.
26+ // In this context, it is calculating the number of seconds that are left over after accounting for the full minutes in the movie length.
27+
28+ // d) Interpret line 4, what does the expression assigned to totalMinutes mean
29+
30+ // It calculates the total number of minutes in the movie by first subtracting the remaining seconds from the total movie length (in seconds)
31+ // and then dividing the result by 60 (the number of seconds in a minute). This gives us the total number of full minutes in the movie length.
2232
2333// e) What do you think the variable result represents? Can you think of a better name for this variable?
2434
35+ // The variable result has the value of a template literal that combines the total hours, remaining minutes, and remaining seconds
36+ // into a string format that represents the length of the movie in hours, minutes, and seconds separated by colons.
37+ // A better name for this variable could be "formattedMovieLength"
38+ // to more clearly indicate that it represents the movie length formatted as hours, minutes, and seconds.
39+
2540// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
41+
42+ // Test 1: movieLength = 1
43+ // Expected output: 0:0:1
44+ // Actual output: 0:0:1
45+
46+ // Test 2: movieLength = -1
47+ // Expected output: 0:0:-1
48+ // Actual output: 0:0:-1
49+
50+ // Test 3: movieLength = "3,600"
51+ // Expected output: type error
52+ // Actual output: NaN:NaN:NaN
53+
54+ // Test 4: movieLength = true
55+ // Expected output: 0:0:1
56+ // Actual output: 0:0:1
57+
58+ // The code will work for all values of movieLength that are of type number or can be coerced to a number (like true which is coerced to 1).
59+ // This is because arithmetic operation need to be performed on movieLength, and if it is not a number or cannot be coerced to a number,
60+ // It will result in NaN (Not a Number) for the calculations of totalMinutes, remainingMinutes, totalHours, and ultimately the result variable.
0 commit comments