@@ -9,17 +9,36 @@ const totalHours = (totalMinutes - remainingMinutes) / 60;
99const result = `${ totalHours } :${ remainingMinutes } :${ remainingSeconds } ` ;
1010console . log ( result ) ;
1111
12- // For the piece of code above, read the code and then answer the following questions
12+ // --- Answers to the questions ---
1313
1414// a) How many variable declarations are there in this program?
15+ // There are 6 variable declarations:
16+ // movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, and result
1517
1618// b) How many function calls are there?
19+ // There is 1 function call:
20+ // console.log(result);
1721
1822// c) Using documentation, explain what the expression movieLength % 60 represents
19- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
23+ // The `%` operator is the modulo operator, which returns the remainder after division.
24+ // So movieLength % 60 gives the remainder when movieLength is divided by 60.
25+ // This means it calculates the leftover seconds after counting full minutes.
2026
2127// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
28+ // Line 4: (movieLength - remainingSeconds) / 60
29+ // This subtracts the leftover seconds from the total seconds,
30+ // giving a multiple of 60 seconds (full minutes),
31+ // then divides by 60 to convert seconds to total minutes.
2232
2333// e) What do you think the variable result represents? Can you think of a better name for this variable?
34+ // result holds the formatted string showing the movie length in "hours:minutes:seconds" format.
35+ // A better name might be `formattedTime` or `movieDurationFormatted`.
2436
2537// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
38+ // For positive integers, the code works correctly.
39+ // For zero, it returns "0:0:0", which is fine.
40+ // For negative numbers, the calculation may produce unexpected negative values.
41+ // For very large values, it still works fine.
42+ // The code assumes movieLength is an integer number of seconds.
43+ // If movieLength is a non-integer or not a number, the code might not behave as expected.
44+
0 commit comments