Code Challenge - Pause Video
by jacobwsmith
JavaScript
/*
Code Challenge - 2019-06-17
DAR viewability measurement produces time series data as a string, based on the measurement of an ad video as it plays.
The string is a list of time segments and each segment has four values:
`[ViewabilityPercentage, PlayheadPosition, Timestamp, SegmentLength]`
The playhead, timestamp and segment length are in seconds. There can be one or many segments.
The tag will mark a pause at the end of the video by adding a 0-second segment.
Examples:
`[[100,0,1517352600,30]]`
1. Segment: Playing 100% in view for 30 seconds
`[[100,0,1517352600,10],[80,10,1517352610,5]]`
1. Segment: Playing 100% in view for 10 seconds
2. Segment: Playing 80% in view for 5 seconds
`[[100,0,1517352600,30],[100,30,1517352630,10],[100,30,1517352640,20]]`
1. Segment: Playing 100% in view for 30 seconds
2. Segment: Pause 100% in view for 10 seconds
3. Segment: Playing 100% in view for 20 seconds
`[[100,0,1517352600,30],[100,30,1517352630,10],[50,30,1517352640,20],[0,50,1517352660,15],[0,50,1517352675,0]]`
1. Segment: Playing 100% in view for 30 seconds
2. Segment: Pause 100% in view for 10 seconds
3. Segment: Playing 50% in view for 20 seconds
4. Segment: Pause 0% in view for 15 seconds
5. Segment: Playing 0% in view for 0 seconds (marks previous segment as pause)
* A pause is signified by the playhead not advancing between two segments, while the timestamp does advance by the segment length.
* The last segment will never be a pause, but its values might mark the second-to-last segment as a pause.
Your challenge:
Given a time series string, find all segments where the video is paused and add up the length of the pauses (adding up their segment lengths). Return the total length of pauses as a number.
*/
// examples
const series = [];
series[0] = '[[100,0,1517352600,30]]';
series[1] = '[[100,0,1517352600,10],[80,10,1517352610,5]]';
series[2] = '[[100,0,1517352600,30],[100,30,1517352630,10],[100,30,1517352640,20]]';
series[3] =
...