Javascript: Sum of intervals

JS: Arrays
Last update: 29 Apr 08:26
0
Students

solution.js

Write and export as default a function that takes an array of intervals and returns the sum of all interval lengths. Only intervals of integers from -100 to 100 are represented as an array used here. The first value of an interval is always less than the second. For example, the length of interval [-100, 0] is 100 and the length of interval [5, 5] is 0. Intersecting intervals must be counted only once.

Examples


sumIntervals([
   [5, 5]
]); // 0

sumIntervals([
   [-100, 0]
]); // 100

sumIntervals([
   [1, 2],
   [11, 12]
]); // 2

sumIntervals([
   [2, 7],
   [6, 6]
]); // 5

sumIntervals([
   [1, 9],
   [7, 12],
   [3, 4]
]); // 11, as in [1,12]

sumIntervals([
   [1, 5],
   [-30, 19],
   [1, 7],
   [16, 19],
   [5, 100]
]); // 130

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
900
exercises
2000+
hours of theory
3200
tests