Calculating seconds: ":2" should be ":02"

I borrowed a script to help me convert a series of digits. However, I’m told that 2 seconds is being displayed as “:2” instead of “:02.” I thought I fixed the problem with a series of conditionals, but that doesn’t seem to have addressed the issue. Can you show me where I went wrong?

...
var minutes = Math.floor(runtime / 60),  // get the minute figure as a digit for the time readout
seconds = runtime % 60; /* use modulus operator to get the remainder and convert into seconds figure for the time readout */
seconds = parseInt(seconds, 10);
                        if (seconds === 0) {
                            seconds = "00";
                        }
                        if (seconds === 1) {
                            seconds = "01";
                        }
                        if (seconds === 2) {
                            seconds = "02";
                        }
                        if (seconds === 3) {
                            seconds = "03";
                        }
                        if (seconds === 4) {
                            seconds = "04";
                        }
                        if (seconds === 5) {
                            seconds = "05";
                        }
                        if (seconds === 6) {
                            seconds = "06";
                        }
                        if (seconds === 7) {
                            seconds = "07";
                        }
                        if (seconds === 8) {
                            seconds = "08";
                        }
                        if (seconds === 9) {
                            seconds = "09";
                        }
var runtime2 = minutes + ":" + seconds; // concatenate everything into minutes and seconds

What’s your runtime variable equating to?

http://codepen.io/ryanreese09/pen/GgewLw

Works fine for me but I had to manually make the runtime variable.

I see the problem already. I had applied the fix to another part of the code without also applying it to this code.

This:

 seconds = parseInt(seconds, 10);
                if (seconds === 0) {
                    seconds = "00";
                }

Should be:

 seconds = parseInt(seconds, 10);
            if (seconds === 0) {
                seconds = "00";
            }
            if (seconds === 1) {
                seconds = "01";
            }
            if (seconds === 2) {
                seconds = "02";
            }
            if (seconds === 3) {
                seconds = "03";
            }
            if (seconds === 4) {
                seconds = "04";
            }
            if (seconds === 5) {
                seconds = "05";
            }
            if (seconds === 6) {
                seconds = "06";
            }
            if (seconds === 7) {
                seconds = "07";
            }
            if (seconds === 8) {
                seconds = "08";
            }
            if (seconds === 9) {
                seconds = "09";
            }

Yes, but did you know you can simply that logic greatly?

if (seconds < 10) {
    seconds = "0" + seconds;
}

which replaces

            if (seconds === 0) {
                seconds = "00";
            }
            if (seconds === 1) {
                seconds = "01";
            }
            if (seconds === 2) {
                seconds = "02";
            }
            if (seconds === 3) {
                seconds = "03";
            }
            if (seconds === 4) {
                seconds = "04";
            }
            if (seconds === 5) {
                seconds = "05";
            }
            if (seconds === 6) {
                seconds = "06";
            }
            if (seconds === 7) {
                seconds = "07";
            }
            if (seconds === 8) {
                seconds = "08";
            }
            if (seconds === 9) {
                seconds = "09";
            }
2 Likes

Not important to this solution, but @StevenHu you should look at switches instead of doing a bunch of if or else if cases.

Use @cpradio’s solution here though.

1 Like

Ha ha … this is really good! I’ll take it. Thanks!

And if you want a single liner, you can use:

seconds = (seconds < 10 ? "0" : "") + seconds;

However, I tend to prefer to extract this out to a more generic padding function:

function pad(n, width, z) {
    z = z || '0';
    n = n + '';
    return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
...
seconds = pad(seconds, 2);

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.