This is very confusing.
First of all, “x” is a terrible name for a variable. We shouldn’t have to guess what it is.
Furthermore, we don’t usually use snake case for JS. I might suggest “durationSeconds”.
Next, what is “sec_d”. Is that a global variable? It is much better to write functions that to not have side effects like that. Your function should return what you want. It gets an input, and from that it generates an output. I would expect something like:
function getSecondsDuration(duration) {
// ...
return secondsDuration
}
// ...
secondsDuration = getSecondsDuration(duration)
But this also raises the question - what is “duration”? I infer that it is a number, it looks like it is the minutes?
When I see something like this: “‘0’+sec_d:sec_d;”, I now think that you want to return a string? In that case, “secondsDuration” is probably a bad name, to me it implies a number. I might call that “durationString”.
So, am I to infer that you want a function that accepts an input of a number of seconds and returns a string in a time format of “mm:ss”? So:
input |
output |
34 |
00:34 |
127 |
02:07 |
792 |
13:12 |
Is that what you are asking for? (If not, please explain the problem in a manner similar to this. This is not an auto shop where you drop off your car to “peons” and dismissively tell them, “fix it, you figure it out”.)
So, that would bring us to something like this:
function getDurationString(duration) {
// ...
return outputString
}
// ...
durationString = getDurationString(duration)
If this is what you want, there are libraries that will do this for you, like moment. You could also use the JS Date to do this. If you want to do it yourself, I would avoid loops. A little math will give you the number of minutes and a little math will give you the number of seconds. Then it is a little formatting, maybe using the JS padStart function to handle those leading 0s.
Is that what you are trying to do?