I have quite a large digital collection of artists (> 1000) and CDs (> 2000) and of course a local SQLite datbase to manage them.
One of the fields in a table is the total runtime of the CD - maybe not a necessary thing to know, but it was an exercise for me, initially in Perl, now in JavaScript.
A fairly simple task is to list the individual track times (an array works good here), but then to get the total hours, minutes, and seconds from that is the exercise.
My first attempt ended up with a simple algorithm:
Then do the same with the minutes, to get TotalHours. Eventually you will get the TotalHours, TotalMinutes, and TotalSeconds.
Calculating minutes from seconds ...
Sprint("<br/>Calculating minutes from seconds ...<br/>");
while(secIN >= 60) {
s = secIN -= 60;
minAgg += 1;
}
minIN += minAgg;
Use the same algorithm to calculate hours from minutes.
This is a different way to achieve the same result, using the modulo operator (%). NO, that's not going to calculate percentage.
Modulus is the REMAINDER after a division, so given a number of seconds, it returns the number of seconds LEFT after dividing minutes by 60.
Then to get the number of minutes from a number of seconds, we need to DIVIDE the seconds by 60, and just get the integer value, using the Math.floor function.
For example, if you have a runtime of 1 hour, 239 minutes, 541 seconds ...