Created
May 21, 2020 16:31
-
-
Save humansonofhuman/1a619e65dc80173ebea1fecfef6c5bbd to your computer and use it in GitHub Desktop.
A code to get the total time of a platzi course from the course page
This file contains hidden or 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 getArrayOfTimes = (timesHtml) => { | |
return [...timesHtml] | |
.map(x => x.innerText) | |
.map(x => { | |
const values = x.split(':',5); | |
return { | |
minutes: parseInt(values[0]), | |
seconds: parseInt(values[1]), | |
}; | |
}); | |
} | |
const reducer = (accumulator, currentValue) => { | |
return { | |
minutes: accumulator.minutes + currentValue.minutes, | |
seconds: accumulator.seconds + currentValue.seconds, | |
}; | |
}; | |
const sumMinsAndSeconds = ({minutes, seconds}) => { | |
minutes = minutes + Math.floor(seconds / 60); | |
hours = Math.floor(minutes / 60); | |
return { | |
hours, | |
minutes: minutes % 60, | |
seconds: seconds % 60, | |
}; | |
}; | |
const getTotalTime = () => { | |
const timesHtml = document.getElementsByClassName("MaterialItem-copy-time"); | |
const times = getArrayOfTimes(timesHtml); | |
const totalTime = sumMinsAndSeconds(times.reduce(reducer)); | |
console.log(`${totalTime.hours}:${totalTime.minutes}:${totalTime.seconds} (hh:mm:ss)`); | |
}; | |
getTotalTime() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment