11//const movieLength = 8784; // length of movie in seconds
2- const movieLength = 9893 ;
3- const remainingSeconds = movieLength % 60 ;
2+ //const movieLength = 9893;
3+ //const movieLength = 223;
4+ //const movieLength = 50;
5+ //const movieLength = 345;
6+ //const movieLength = 600;
7+ //const movieLength = 400;
8+ //const movieLength = 90;
9+ //const movieLength = 189;
10+ function MovieFormatting ( num ) {
11+ if ( num < 10 ) {
12+ var result = 0 + num . toString ( ) ;
13+ }
14+ else {
15+ return num . toString ( ) ;
16+ }
17+ return result ;
18+ }
19+
20+ const movieLength = 800 ;
21+ const remainingSeconds = movieLength % 60 ;
422const totalMinutes = ( movieLength - remainingSeconds ) / 60 ;
523
624const remainingMinutes = totalMinutes % 60 ;
725const totalHours = ( totalMinutes - remainingMinutes ) / 60 ;
8-
9- const result = `${ totalHours } :${ remainingMinutes } :${ remainingSeconds } ` ;
26+ const result = `${ MovieFormatting ( totalHours ) } :${ MovieFormatting ( remainingMinutes ) } :${ MovieFormatting ( remainingSeconds ) } ` ;
1027console . log ( result ) ;
1128
1229// For the piece of code above, read the code and then answer the following questions
@@ -26,7 +43,10 @@ console.log(result);
2643Firstly it will evaluate bracket i.e (movieLength - remainingSeconds) and then divide the value by 60.
2744*/
2845// e) What do you think the variable result represents? Can you think of a better name for this variable?
29- // The result represents the how long the movie is, in hours, minutes and seconds. It can named as MovieDuration .
46+ // The result represents the how long the movie is, in hours, minutes and seconds. It can named as "Duration" .
3047// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
31- // Yes, I changed the value of movieLength and it worked. The movieLength is the variable used in calculating other
32- // values and to evaluate the total length of movie.
48+ // Yes, the code works for different values of movieLength and does not produce any errors. However, it does not always format
49+ // the output correctly. If the hours, minutes, or seconds are less than 10(e.g 5, 7), they are displayed as a single
50+ // digit instead of two digits. The standard way to display a movie duration is 00:00:00, so I created a function that
51+ // adds a leading 0 when a value is less than 10. Otherwise, it returns the value as a string.
52+ // After creating a function, I test my code with multiple values of movieLength and it passed all the test cases.
0 commit comments