Skip to content

Instantly share code, notes, and snippets.

@humansonofhuman
Created May 21, 2020 16:31
Show Gist options
  • Save humansonofhuman/1a619e65dc80173ebea1fecfef6c5bbd to your computer and use it in GitHub Desktop.
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
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