/*
14. You are given an input array of the intervals on the number axis. For each interval a start point, end point and some “value” is known. Write a function in pure JavaScript, that will transform the input array to another array of the objects with the same structure. Function should sum the “values” of all intersecting intervals and include in the result all intervals with different sum of “values”. See the examples of input and expected output. Note, that start and end point coordinates may have fractional part.
var input = [{
start : 1,
end : 2,
value : 1
}, {
start : 2,
end : 3,
value : 1
}]
var output = [{
start : 1,
end : 3,
value : 1
}]
----------------------
var input = [{
start : 1,
end : 3,
value : 1
}, {
start : 2,
end : 4,
value : 1
}]
var output = [{
start : 1,
end : 2,
value : 1
}, {
start : 2,
end : 3,
value : 2
}, {
start : 3,
end : 4,
value : 1
}]
--------------------------
var input = [{
start : 1,
end : 3,
value : 1
}, {
start : 2,
end : 4,
value : 1
}, {
start : 2,
end : 5,
value : 2
}, {
start : 4,
end : 5,
value : 1
}]
var output = [{
start : 1,
end : 2,
value : 1
}, {
start : 2,
end : 3,
value : 4
}, {
start : 3,
end : 5,
value : 3
}]
*/
function splitAndSum(input) {
var keys = {};
for (var i=0, iMax=input.length; i<iMax; i++) {
keys[input[i].start] = true;
keys[input[i].end] = true;
}
var points = [];
for (var k in keys) {
if (keys.hasOwnProperty(k)) {
points.push(parseFloat(k));
}
}
points.sort(function(a, b) {
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.