I made a JavaScript loop that skips every set of two numbers that are followed by a set of three.
Consider the following set of integers:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
This is what the array returns:
1, 2, 3, 6, 7, 8
See how it skipped 4-5 and 9-10? (every set of two numbers followed by a set of three)
Visual:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 (skips bolded numbers)
Here is what I came up with:
var y = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (var i = 0; i < y.length; i++) {
if (y[i] >= 4 && y[i] <= 5) {
continue;
}
if (y[i] >= 9 && y[i] <= 10) {
continue;
}
document.write(y[i]);
}
The above for-loop returns 123678
from 12345678910
. The problem is that I had to manually say which ones to skip. Is there a way, possibly with %
modulus, to automatically skip every set of two numbers followed by a set of three without limiting the length to ten?
You could just keep a counter instead
for (var i=0, j=0; i < y.length; i++) {
if (j < 3) {
document.write(y[i]);
} else if ( j > 3) {
j = -1;
}
j++;
}