0

I thought that it is simple task but unfortunatelly I can not do it properly without too much if'ology.

I have following data structure. I have a "big period" which starts at 1 and ends at 30.Each object represents "good" period which starts at object.start and ends at object.ends. "Bad" periods are between the "good" ones and could be between 1 and first period and last period and 30.

const data = [
  {start: 2, end: 4},
  {start: 10, end: 15},
  {start: 17, end 27}
]

I would like to get new array of "bad" and "good" periods lengths. Each array item represents length of period (bad or good one)

array = [1, 3, 5, 6, 1, 11, 3]

as a result of

[from 1 to 1 (bad period), from 2 to 4 (good period), from 5 to 9(bad period), from 10 to 15(good period), from 16 to 16(bad period), from 17 to 27(good period), from 28 to 30(bad period)] 

My main if'ology starts when I check if first object.start is 0 or not and last object.end is 30 or not. How to avoid it?

3
  • 2
    I'm totally unclear on what you mean or how you get from that input to that output. Can you say more or give a clearer example? Commented Apr 11, 2017 at 18:46
  • 1
    Please post your code, and please describe the result your trying to achieve... we see you have a const array called data with properties of start and end but what are you trying to do? Commented Apr 11, 2017 at 18:47
  • I added more explanations Commented Apr 11, 2017 at 18:59

1 Answer 1

1

You could check if the first start value is smaller than the wanted interval start and insert the length of the bad period into the result array and then add the good period length to the result array. Adjust the last with the end, then proceed.

At the end take the rest of the needed interval and push the length to the result array.

                                                 miss.   given
data                       missing     given    length  length
------------------------- ---------  ---------  ------  ------
const data = [             
  {start: 2 , end:  4},     1 -  1     2 -  4      1       3
  {start: 10, end: 15},     5 -  9    10 - 15      5       6
  {start: 17, end: 27}     16 - 16    17 - 27      1      11
]                          28 - 30                 3

var data = [{ start: 2, end: 4 }, { start: 10, end: 15 }, { start: 17, end: 27 }],
    last = 1,
    end = 30,
    result = [];

data.forEach(function (o) {
    if (last < o.start) {
        result.push(o.start - last);
    }
    result.push(o.end - o.start + 1);
    last = o.end + 1;
});

if (last < end) {
    result.push(end - last + 1);
}

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very very much. Very clean sloution and works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.