0

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];
    };
})();
2
  • 1
    Were have you found that?and copy + paste does not count as a try Commented Mar 16, 2018 at 20:29
  • Jonas not a copy and paste. Got it off this site, can't find it now. Thought it was working when I built a temporary jsfiddle for it; but can't get my head around a fix for it. Commented Mar 16, 2018 at 20:32

5 Answers 5

2

If you can absolutely rely on the algorithm that populates your array, following the stated rules

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].

Then you all you need to do is check that the last element of the array is the same value as the length of the array:

var good = [1, 2, 3];
var bad = [1, 2, 3, 5];

var isValid = function(arr) {
  return (arr.length > 2 && arr[arr.length - 1] === arr.length); // Thanks to Jonas W.
}
console.log(isValid(good));
console.log(isValid(bad));

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

2 Comments

what if a value is duplicated ? like this bad=[1,2,2,4] ?
That's not how the algorithm is supposed to work. (read the question) The answer is only appropriate for this specific question.
1

You could use a closure over the first index value and increment that value while checking.

function isConsecutive(array) {
    return array.length >= 2 && array.every((v => a => a === v++)(array[0]));
}

console.log([[1, 2, 3, 4], [1, 2, 3, 5]].map(isConsecutive));

Comments

0

var array=[5,6,7,8];

function continuous(arr){
  var max = Math.max(...arr);
  var min = Math.min(...arr);

  return (((max-min)+1) * (min + max) / 2) == arr.reduce((a, b) => a + b, 0)
}

console.log(continuous(array))

Comments

0
 Array.prototype.isConsecutive = function(){
   for(var i = 1; i < this.length; i++)
     if(this[i] !== this[i - 1] + 1)
        return false;
   return true;
 };

Or the obfuscated version:

  const isConsecutive = arr => !!arr.reduce((prev, curr) => curr === prev + 1 ? curr : 0);

Comments

0

You can use this too, it is recursive:

Array.prototype.isConsecutive = function(){
if (arguments.length==0) return this.isConsecutive(0);
else if (arguments[0]>=this.length) return true;
else return (this[arguments[0]]==arguments[0]+1&&this.isConsecutive(arguments[0]+1
));}
cons=[1,2,3];
console.log(cons.isConsecutive());
non_cons=[1,2,2,4];
console.log(non_cons.isConsecutive());

Comments

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.