lundi 16 août 2021

function that converts any number into time format in javascript [duplicate]

I'm trying to create a function that takes in a number and converts it into hours and minutes. The problem is that that code is not printing 0 as singular for example "0 minute" not "0 minutes" same for hours. here is my code:

function convertNumToTime(num) {
  var hour = Math.floor(num / 60);
  var minutes = (num % 60);

  var hourText;
  if (hour <= 1) {
    hourText = "hour";
  } else {
    hourText = "hours";
  }
  var minuteText;
  if (minutes <= 1) {
    minuteText = "minute";
  } else {
    minuteText = "minutes";
  }

  console.log(hour + " " + hourText + ", " + minutes + " " + minuteText);
}

convertNumToTime(176)
convertNumToTime(0)

Aucun commentaire:

Enregistrer un commentaire