2

I'd like my countdown timer to show double digits, like "53:00" instead of "53:0". Not sure what's going wrong here? Thank you!

function convertSeconds(s) {
    var min = Math.floor(s / 60);
    var sec = s % 60;
    return nf(min, 2) + ':' + nf(sec, 2);
  }
Pier Stein
  • 49
  • 3
  • 6
    Well that depends on whatever `nf()` is - you need to add that code to your question. – Alex K. Mar 25 '19 at 16:14
  • Thought that was numberformat? Do I need to make it its own function and refer to that with nf? What extra code do you expect to see? Srry, new to coding. Thanks for your time! – Pier Stein Mar 25 '19 at 16:17
  • If your code works without errors then there is an `nf()` function written somewhere - in a library you have included for example, its not a built in part of JavaScript. – Alex K. Mar 25 '19 at 16:19
  • `nf` is not a built-in Javascript function, you will need to show where it's defined, otherwise it's impossible to tell what it is and what it does. – Etheryte Mar 25 '19 at 16:19
  • Possible duplicate of [How to format numbers by prepending 0 to single-digit numbers?](https://stackoverflow.com/questions/8043026/how-to-format-numbers-by-prepending-0-to-single-digit-numbers) – feeela Mar 25 '19 at 16:23

4 Answers4

2

To pad zeroes you can do

function nf(num, places) {
  var s = '0' + num;
  return s.slice(places * -1);
}

function convertSeconds(s) {
  var min = Math.floor(s / 60);
  var sec = s % 60;
  return nf(min, 2) + ':' + nf(sec, 2);
}

Should get you what you want

inorganik
  • 24,255
  • 17
  • 90
  • 114
  • 1
    There must be an nf() somewhere in your code stack, I would the code above with a different name as nf()'s current behavior/dependencies are unknown. – Alex K. Mar 25 '19 at 16:28
2

You can use padStart() to make sure a string is a certain length. If it's not it will pad it with whatever you want. In this case 0:

const nf = (n, c) => n.toString().padStart(c, '0');

function convertSeconds(s) {
  var min = Math.floor(s / 60);
  var sec = (s % 60)
  return nf(min, 2) + ':' + nf(sec, 2);
}

console.log(convertSeconds(64))
console.log(convertSeconds(119))

Not sure if you want to pad the minutes of not.

There is also the Intl.NumberFormat() which has an option for minimum digits (though this seems like overkill for this):

console.log(new Intl.NumberFormat('en-US', { minimumIntegerDigits: 2}).format(2));

console.log(new Intl.NumberFormat('en-US', { minimumIntegerDigits: 2}).format(59));
Mark
  • 90,562
  • 7
  • 108
  • 148
1

The nf() function should be like this:

function nf(num){
    if(num < 10){
        return "0"+num;
    }
    return num;
}

console.log(nf(7));
// 07
console.log(nf(11));
// 11
Adnan Toky
  • 1,756
  • 2
  • 11
  • 19
  • You don't need to convert to a string for the condition. `return n <= 9 ? '0' + n : n;` will be a slightly shorter way… – feeela Mar 25 '19 at 16:25
1

If by nf() you mean a function that formats a Number as string of given digits, this can be an implementation of it:

function nf(number, digits) {
  var res = number.toString();
  while (res.length < digits) {
    res = "0" + res;
  }
  return res;
}

Demo:

function convertSeconds(s) {
  var min = Math.floor(s / 60);
  var sec = s % 60;
  return nf(min, 2) + ':' + nf(sec, 2);
}

function nf(number, digits) {
  var res = number.toString();
  while (res.length < digits) {
    res = "0" + res;
  }
  return res;
}

console.log(nf(4,2));
cнŝdk
  • 31,391
  • 7
  • 56
  • 78