michael-h.dk

Multimedia Designer and Web Developer

Photo By Jon Flobrant
        from Unsplash.com

Tally String Times with Reduce

A javascript program that take the time information from video clips, and add them up to a total time.

The challenge is that the time codes are hidden in the HTML, so we have to get that out, and turned into an array, that we can map over and run the reduce function on.

In code it looks like this:

const seconds = timeData
.map(node => node.dataset.time)
.map(timeCode => {
[mins, secs] = timeCode.split(':')
.map(parseFloat);
return (mins * 60) + secs;
})
.reduce((total, vidSeconds) => total + vidSeconds);

The Total Time Is:

Test time

The Data Set
Video 1 5:43
Video 2 2:33
Video 3 3:45
Video 4 0:47
Video 5 5:21
Video 6 6:56
Video 7 3:46
Video 8 5:25
Video 9 3:14
Video 10 3:31
Video 11 5:59
Video 12 3:07
Video 13 11:29
Video 14 8:57
Video 15 5:49
Video 16 5:52
Video 17 5:50
Video 18 9:13
Video 19 11:51
Video 20 7:58
Video 21 4:40
Video 22 4:45
Video 23 6:46
Video 24 7:24
Video 25 7:12
Video 26 5:23
Video 27 3:34
Video 28 8:22
Video 29 5:17
Video 30 3:10
Video 31 4:43
Video 32 19:43
Video 33 0:47
Video 34 0:47
Video 35 3:14
Video 36 3:59
Video 37 2:43
Video 38 4:17
Video 39 6:56
Video 40 3:05
Video 41 2:06
Video 42 1:59
Video 43 1:49
Video 44 3:36
Video 45 7:10
Video 46 3:44
Video 47 3:44
Video 48 4:36
Video 49 3:16
Video 50 1:10
Video 51 6:10
Video 52 2:14
Video 53 3:44
Video 54 5:05
Video 55 6:03
Video 56 12:39
Video 57 1:56
Video 58 4:04