@@ -9,17 +9,40 @@ 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
13-
1412// a) How many variable declarations are there in this program?
13+ // Declarations are lines that use const or let
14+ // const movieLength
15+ // const remainingSeconds
16+ // const totalMinutes
17+ // const remainingMinutes
18+ // const totalHours
19+ // const result
20+ // My answer: 6 variable declarations
1521
1622// b) How many function calls are there?
23+ // A function call uses parentheses like something(..)
24+ // console.log(result) is a function call
25+ // My answer: 1 function call
1726
1827// c) Using documentation, explain what the expression movieLength % 60 represents
19- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
28+ // % is the remainder operator
29+ // movieLength % 60 gives the leftover seconds after dividing by 60
30+ // (the seconds part that doesn't make a full minute)
31+ // Answer: it represents the remaining seconds
2032
2133// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
34+ // totalMinutes = (movieLength - remainingSeconds) / 60
35+ // First it removes the leftover seconds so we have an exact number of seconds that fits into whole minutes
36+ // Then it divides by 60 to convert seconds into minutes
37+ // Answer: it calculates the total whole minutes in the movie
2238
2339// e) What do you think the variable result represents? Can you think of a better name for this variable?
40+ // result is a string that formats the time as hours:minutes:seconds
41+ // Better name: formattedTime or movieDuration
2442
2543// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
44+ // It works for normal positive numbers (seconds) like 8784
45+ // It will also run for 0 (gives 0:0:0)
46+ // But negative values would produce negative hours/minutes/seconds, which isn't a real time format
47+ // So it assumes movieLength is a non-negative number
48+
0 commit comments