Drill

ProblemsTypeScript › dates

Say a duration the way a person would

mediumdatesTypeScript

A ticket screen shows how long something took, and "90m" reads worse than "1h 30m".

minutesToText(minutes: int) → string

Solve it in the editor →

Where you start

function minutesToText(minutes: number): string {
  
}

Worked examples

CallResult
minutesToText(90)"1h 30m"
minutesToText(120)"2h"
minutesToText(45)"45m"
minutesToText(0)"0m"

Hint

Work out both parts, then decide which ones to print.

Reference solution in TypeScript
function minutesToText(minutes: number): string {
  if (minutes <= 0) return '0m';
  const h = Math.floor(minutes / 60);
  const m = minutes % 60;
  if (h === 0) return m + 'm';
  if (m === 0) return h + 'h';
  return h + 'h ' + m + 'm';
}

The same problem in another language

More dates problems in TypeScript