JavaScript Number Type
Arithmetic
We can perform all kinds of calculations on numbers by using the arithmetic operator JavaScript provides.
const addition = 1 + 2 // 3const subtraction = 1 - 2 // -1const multiplication = 1 * 2 // 2const division = 1 / 2 // 0.5const remainder = 9 % 4 // 1
These work pretty much as we've been taught in school.
Test yourself
Description
Define a new function with the name
minutesToDaysHoursMinutes
that takes a single integer number input parameter: minutes. Your
function should convert the amount of minutes to a combination of
days, hours and minutes and return an object with the keys:
days: number of whole dayshours: number of whole hours (must be less than 24)minutes: number of whole minutes (must be less than 60)
We'll learn more about objects later on, but for now you can use the base code provided and bind your calculated values to the appropriate constants.
Tips:
- There are 60 minutes in an hours.
- There are 24 hours in a day.
- There are 1440 minutes in a day.
- Store intermediate calculations in variables.
- Calculation of whole days (quotient):
- Number of minutes
/minutes in a day - Round it down using
Math.floor
- Number of minutes
- Calculation of remaining hours (remainder):
- Number of minutes
%minutes in a day
- Number of minutes
- Calculation of whole hours (quotient):
- Number of remaining minutes
/minutes in a hour - Round it down using
Math.floor
- Number of remaining minutes
- Calculation of remaining minutes (remainder):
- Number of remaining minutes
%minutes in a hour
- Number of remaining minutes
Tests
- 1
You should define a function named
minutesToDaysHoursMinutes. - 2
minutesToDaysHoursMinutes(60)should return{"days":0,"hours":1,"minutes":0}. - 3
minutesToDaysHoursMinutes(90)should return{"days":0,"hours":1,"minutes":30}. - 4
minutesToDaysHoursMinutes(560)should return{"days":0,"hours":9,"minutes":20}. - 5
minutesToDaysHoursMinutes(2634)should return{"days":1,"hours":19,"minutes":54}. - 6
minutesToDaysHoursMinutes(1051408)should return{"days":730,"hours":3,"minutes":28}.