Skip to content

Instantly share code, notes, and snippets.

@SeeThruHead
Created October 28, 2015 20:05
Show Gist options
  • Save SeeThruHead/43df70083617fbd9e07b to your computer and use it in GitHub Desktop.
Save SeeThruHead/43df70083617fbd9e07b to your computer and use it in GitHub Desktop.
var durations = ["12:38", "6:36", "9:03", "8:34", "5:02", "6:54", "13:22", "4:41", "8:36", "21:58", "3:06", "10:46", "10:13", "12:54", "14:00", "11:03", "16:03", "10:52", "24:53", "10:03", "11:49", "15:47", "3:19", "2:06", "5:47", "1:03", "5:29", "5:47", "26:39"];
// write a function that will take the above array of string durations and convert it hours/mins/seconds
// You can use any JS you want - loops/map/reduce/etc...
function addDurations(acc, curr) {
const [ seconds = 0, minutes = 0, hours = 0] = curr.split(':')
.reverse().map((x) => parseInt(x, 10));
return acc += hours * 3600 + minutes * 60 + seconds;
}
function formatTime(seconds) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor(seconds % 3600 / 60);
return `${hours}:${minutes}:${seconds % 60}`;
}
function accTime(durations) {
const seconds = durations.reduce(addDurations, 0);
return formatTime(seconds);
}
console.log(accTime(durations));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment