Problem overview
The starter code stores how long someone worked on a project, as a number of hours and a number of minutes. The task is to print the total time in seconds.
let hours: number = 13;
let minutes: number = 47;
The example in the task uses 1 hour and 2 minutes. That is 62 minutes, and since a minute has 60 seconds, it is 3720 seconds. The goal is an expression that does this for any values of hours and minutes.
Thinking in units
Converting between units is a chain of multiplications. The easiest way to avoid mistakes is to convert one unit at a time and keep track of what each intermediate number measures.
- Hours to minutes. One hour is 60 minutes, so
hours * 60is the number of minutes in the hours part. For 13 hours that is 780 minutes. - Add the minutes. Both parts are now in minutes, so they can be added:
hours * 60 + minutes. That gives 827 minutes. You can only add two quantities when they have the same unit. - Minutes to seconds. One minute is 60 seconds, so the total number of minutes is multiplied by 60.
Step 3 needs the whole sum from step 2, so the sum goes in parentheses:
let hours: number = 13;
let minutes: number = 47;
console.log((hours * 60 + minutes) * 60);
It prints:
49620
Check by hand: 827 minutes times 60 is 49620 seconds.
Why the parentheses matter
Multiplication runs before addition. Without the parentheses, hours * 60 + minutes * 60 would be read as (hours * 60) + (minutes * 60). That converts only the minutes to seconds and leaves the hours in minutes, mixing two units. For the example of 1 hour and 2 minutes, the mistaken expression gives 180 instead of 3720.
The outer expression is a product of a sum and 60, and the only way to say "add first, then multiply" is with parentheses. Another correct form multiplies each part by its own factor, converting hours straight to seconds with 3600:
console.log(hours * 3600 + minutes * 60); // 49620
Both forms give the same result. The lesson's form builds the answer from the 60-to-1 relationship twice, which is easier to remember than the number 3600.
Notes about numbers
- All the values are
number. The type has no separate integer form, so the code would also work with fractional input, such as 1.5 hours. The result may then show decimals. - Nothing overflows. The result of an exercise like this is far below the limit where whole numbers stop being exact, which is about 9 quadrillion.
- Name the pieces if it helps. You may store the intermediate value in a variable, such as
const totalMinutes: number = hours * 60 + minutes;, and then printtotalMinutes * 60. The result is the same, and the code explains itself.
Common mistakes
- Missing parentheses.
hours * 60 + minutes * 60mixes units, as shown above. - Multiplying by 60 only once.
hours * 60 + minutesis 827, the number of minutes. The task asks for seconds. - Using 100 instead of 60. Time units are not decimal. An hour has 60 minutes, not 100.
- Printing a string. Writing the expression inside quotes prints the text of the expression, not its value.
- Typing 13 and 47 into the expression. Use
hoursandminutes, so the answer follows the variables.
Testing
hours = 1,minutes = 2gives3720.hours = 0,minutes = 5gives300.hours = 2,minutes = 0gives7200.hours = 13,minutes = 47gives49620.
Key takeaways
- Convert one unit at a time, and add only quantities that share a unit.
- Parentheses make an addition run before the multiplication that follows it.
- Check your expression against the task's example by hand.