Skip to content

Latest commit

 

History

History

18 - Adding Up Times with Reduce

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Adding Up Times With Reduce

An ul representing a playlist where each li represents a video and it has a data-time attribute with its duration(string). Calculate the total time in hours:minutes:seconds

Notes:

  • Two implementations, one is self-describing the other is serious stuff 😝
        const timeAttributes = Array.from(document.querySelectorAll('[data-time]'));
    
        const totalSeconds = timeAttributes
            .map(node => node.dataset.time)
            .map(timeCode => {
            const [mins, secs] = timeCode.split(':').map(parseFloat);
            return mins * 60 + secs;
            })
            .reduce((a, b) => a + b);
        const timeAttributes = Array.from(document.querySelectorAll('[data-time]'));
    
        const totalSeconds = timeAttributes
            .reduce((a, b) => {
                b = b.dataset.time.split(':').map(parseFloat);
                return a + b[0]*60 + b[1];
            }, 0);
  • There is also a function that converts from seconds to a hours:minutes:seconds format.