Created
November 15, 2018 17:32
-
-
Save danvega/1ed592e40a198d52808773ee261c62c0 to your computer and use it in GitHub Desktop.
Teachable Course Length
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const lessons = document.querySelectorAll('.lecture-name'); | |
let durations = []; | |
lessons.forEach((lesson) => { | |
const name = lesson.innerText; | |
const start = name.indexOf("("); | |
const end = name.indexOf(")"); | |
const time = name.substring(start + 1,end); | |
if( time != '' && time != undefined && time.indexOf(":") > 0 ) { | |
durations.push(time); | |
} | |
}); | |
console.log(formatDuration(sumDurations(durations))); | |
function sumDurations(durations) { | |
return durations.reduce((sum, string) => { | |
var mins, secs; | |
[mins, secs] = string.split(":").slice(-2).map(n => parseInt(n, 10)); | |
return sum + mins * 60 + secs; | |
}, 0); | |
} | |
function formatDuration(duration) { | |
function pad(number) { | |
return `${number}`.slice(-2); | |
} | |
let hours = duration / 3600 | 0; | |
let minutes = duration % 3600 / 60 | 0; | |
let seconds = duration % 60; | |
let minsSecs = `${pad(minutes)}:${pad(seconds)}`; | |
return hours > 0 ? `${hours}:${minsSecs}` : minsSecs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is awesome