For a given range of numbers, how can I skip two numbers and then get the next three numbers?
e.g. For the range 0..20
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
for (let list = 0; list < 100; list ) {
console.log(list);
}
I need to get this type of result:
2, 3, 4, 7, 8, 9, 12, 13, 14....
CodePudding user response:
Loop through from your start
to the end
. Use modulus arithmetic to determine which part of the 5 number cycle the number lies on. If it's the 0th, or 1st then ignore, otherwise add them to your result:
let start = 0;
let end = 20;
let result=[];
for(let i=0; i<(end-start); i )
{
let mod = i % 5;
switch(mod)
{
case 0:
case 1:
// Ignore these numbers
break
case 2:
case 3:
case 4:
result.push(start i);
break;
}
}
console.log(result);