JavaScript Number Type

Arithmetic

We can perform all kinds of calculations on numbers by using the arithmetic operator JavaScript provides.

const addition = 1 + 2 // 3
const subtraction = 1 - 2 // -1
const multiplication = 1 * 2 // 2
const division = 1 / 2 // 0.5
const 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:

  1. days: number of whole days
  2. hours: number of whole hours (must be less than 24)
  3. 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):
    1. Number of minutes / minutes in a day
    2. Round it down using Math.floor
  • Calculation of remaining hours (remainder):
    1. Number of minutes % minutes in a day
  • Calculation of whole hours (quotient):
    1. Number of remaining minutes / minutes in a hour
    2. Round it down using Math.floor
  • Calculation of remaining minutes (remainder):
    1. Number of remaining minutes % minutes in a hour

Tests

  1. 1 You should define a function named minutesToDaysHoursMinutes.
  2. 2minutesToDaysHoursMinutes(60) should return {"days":0,"hours":1,"minutes":0}.
  3. 3minutesToDaysHoursMinutes(90) should return {"days":0,"hours":1,"minutes":30}.
  4. 4minutesToDaysHoursMinutes(560) should return {"days":0,"hours":9,"minutes":20}.
  5. 5minutesToDaysHoursMinutes(2634) should return {"days":1,"hours":19,"minutes":54}.
  6. 6minutesToDaysHoursMinutes(1051408) should return {"days":730,"hours":3,"minutes":28}.
Editor