Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Js to generate a sequence of array

I am give start and the end and the size, I am required to generate array as in illustration

when

start = 1

end = 100

size = 10

the expected result is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
  [10,20],[20,30],[30,40],[40,50],[50,60],[60,70],[70,80],[80,90],[90,100]
]

UPDATE

second example

When

start = 1

    end = 100
    
    size = 10

expected result

[[5,10],[10,15],[15,20],[20,25],....[90-95],[95,100]]

I have tried with,

var start = 1;
end = 100
size = 10;
result = []
for (var i = start; i <= end; i+=size) {
    result.push([i, i + size-1])
}

console.log(JSON.stringify(result))

>Solution :

Simply changing [i, i + size-1] to [i - 1, i + size-1] seemed to work just fine. The first element in the array doesn’t match your example, but it can easily be removed. Full code below.

var start = 1;
end = 100
size = 10;
result = []
for (var i = start; i <= end; i += size) {
  if (i <= start * 10) { 
    end += 10;
    continue;
  }
  result.push([i - start, i + 9 - (start - 1)])
  if (i > 200) break
}

console.log(JSON.stringify(result))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading