Could anyone help me solve a javascript problem?


	 function get_sec_d (x) {
		if(Math.floor(x) >= 60){
			
			for (var i = 1; i<=60; i++){
				**if(Math.floor(x)>=(60*i) && Math.floor(x)<(60*(i+1))) {**
					sec_d = Math.floor(x) - (60*i);**
					sec_d = sec_d <10 ? '0'+sec_d:sec_d;**
				}
			}
		}else{
		 	sec_d = (isNaN(duration) === true)? '0':
		 	Math.floor(x);
		 	sec_d = sec_d <10 ? '0'+sec_d:sec_d;
		 }
	} 

	
	// define seconds duration
	
	get_sec_d (duration);

Set the duration is 200 seconds and i = 2.
then x = 200 seconds and i = 2

math.floor(200) >= 120 && math.floor(200) < (180)

How it can operate?

Source Code :https://codepen.io/KenJP/pen/wvmvWPg

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

do you mean that this code is a mistake?

Without knowing what you expect the code to do we don’t if there is a mistake or not

This line of code is defining minutes duration of a track. I posted a full set of source code. You can pore over the source code.

It would be helpful if you explain more, I don’t understand what kind of help are you asking.

the full set of code is a music player. the line of code I posted is defining the minutes duration of a music. I would like someone who can explain the following situation.

Set the music duration is 200 seconds and i = 2.

then x = 200 seconds and i = 2

			for (var i = 1; i<=60; i++){
				**if(Math.floor(200)>=(120) && Math.floor(200)<180)) {**
					sec_d = Math.floor(200) - (120);**
					sec_d = sec_d <10 ? '0'+sec_d:sec_d;**
				}
			}

math.floor(200) >= 120 && math.floor(200) < (180)

Because 200 is larger than 180 and the “for loop” is not working.
How the code can define music minute if math.floor(200) < (180)?

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?

Thank you for your opinion. I am trying to understand what you expressed.

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