didnt answer the question of where you did get it…
your answer copies and pastes those formulas… ‘your’ last one is just the first code with Archibald’s first correction in place without being able to construct the missing lines. You’ve shown no learning there.
Now hes given you every line you need to fix the other site’s code. But lets try one more time before you simply take Archibald’s code and run away again: why does this code work? What would you do if asked to give the number of days?
Of course, from StackOverflow. It’s okay for any developer to do that !!!
But I don’t copy and paste directly. I take the code to understand, then use it.
So I pasted the code here to understand this code and learn something new. You accused me of stealing that code, and that’s totally unacceptable. We are here to get feedback and learn.
I tried that.
For example, If the seconds are 359999 and I want to turn to minutes, I divide by 60, the result 5999.983333333334. But I want the minutes that come after hours, so I use % operator and then the result is 59.983333333333576. Finally, I use Math.floor or parseInt to have a whole number.
If you’ve absorbed the idea of what the divisor/modulo operator does, you can generate your own answer to the coding challenge, which is what it’s designed to do.
To go back to your original post of not understanding a line like this:
We need to go back to the original source of the code.
Note that this code was originally developed to look for the number of YEARS first (which is where 31536000 comes from - number of seconds in a year), and was not bound by constraints such as “it will be less than 100 hours”.
Archibald is correct in that the later lines can be simplified - but only because every larger value’s modulo is a multiple of the smaller one.
X % 3600 % 60 is redundant; 3600 is a multiple of 60, so you can just say X % 60 and get the answer immediately.
Also, as all things with time goes, finding the ‘number of years’ by doing simple math is inevitably going to go wrong - because you have to account for things like leap years. Not every year is 3156000 seconds long.
As you have three “if” statements there all doing essentially the same thing, you could consider calling a function three times to format the numbers to be two digits. Here is the code I wrote earlier to solve the challenge (which also more explicitly creates strings representing the numbers):
function humanReadable(seconds) {
let hrs = format(Math.floor(seconds / 3600));
let mins = format(Math.floor(seconds / 60 % 60));
let secs = format(seconds % 60);
return( hrs + ":" + mins + ":" + secs );
}
function format(x) {
let s = x.toString();
if (s.length < 2) s = "0" + s;
return s;
}
There are enthusiasts in this forum who like to have numerous functions, even if some functions are only two or three lines long. My own preference tends to be to keep a function such as this together and not to call any other small functions. This way the code can be more readable and it saves you having to find elsewhere in your code the subfunctions being called.
Both of us should have added comments to our code .
The challenge is about you have seconds any number of seconds, and you want to turn seconds to human-readable time.
When humanReadable(3601), the output is: 01:00:01
well no, you have seconds <= 359999, but thats the thrust of the challenge, yes.
What I was pointing out is that the code that you originally quoted was designed not for this challenge, but for a general 'how do i convert seconds to years/days/hours/minutes/seconds, and that that code will have problems with the number of seconds being more than 4 years.