I build up an array with a counter when a function is true.
So if it were true 3 times in a row, the array looks like [1,2,3]. If the function is not true there is a gap in the counter and could look like this [1,2,3,5].
In another function I need to determine if the array length is > 2 and the values in the array are in consecutive order. So [1,2,3] it should return true. If [1,2,3,5] it should return false.
I haven't found anything that's worked. Any help with a possible solution would be much appreciated.
I have seen this (and have tried it) but it doesn't work.
Array.prototype.is_consecutive = (function () {
var offset = 0; // remember the last offset
return function () {
var start = offset, len = this.length;
for (var i = start + 1; i < len; i++) {
if (this[i] !== this[i - 1] + 1) {
break;
}
}
offset = i;
return this[start];
};
})();