Write a function to determine whether an array contains consecutive numbers for at least N numbers
One issue to consider: what if an element in the array is 0, and thus falsey? Then if (prev &amp;&amp; will not be fulfilled:

console.log(hasConsecutiveNums([-1, 0, 1], 3)) // false... oops




function hasConsecutiveNums(array, N) {
  if (array.length &lt; N) return false;
  if (N === 0) return true;
  const sortedArray = array.slice().sort((a, b) =&gt; a - b);
  let count = 0;
  let prev = null;
  for (const num of sortedArray) {
    if (prev &amp;&amp; num === prev + 1) {
      count++;
    } else {
      count = 1;
    }
    if (count === N) return true;
    prev = num;
  }

  return false;
}

console.log(hasConsecutiveNums([-1, 0, 1], 3)) // false... oops




Another tweak to make the code a bit more elegant would be to assign prev to the first element of the array first, and initialize count to 1, thus starting comparison on the second element rather than on the first, avoiding the need to compare against null.

With this method, you also need to return true immediately if the array's length is only 1, like the other answer recommends, otherwise there won't be any iterations within which return true could be reached:



function hasConsecutiveNums(array, N) {
  if (array.length &lt; N) return false;
  if (N &lt;= 1) return true;
  const sortedArray = array.slice().sort((a, b) =&gt; a - b);
  let prev = sortedArray.shift();
  let count = 1; // first element of the array is already in prev
  for (const num of sortedArray) {
    if (num === prev + 1) {
      count++;
    } else {
      count = 1;
    }
    if (count === N) return true;
    prev = num;
  }

  return false;
}

console.log(hasConsecutiveNums([1, 4, 5, 6], 3)) // true
console.log(hasConsecutiveNums([1, 4, 5, 6], 4)) // false
console.log(hasConsecutiveNums([-1, 0, 1], 3)) // true




If, as the comment notes, you'd want [1, 2, 2, 3] to return true, de-duplicate the numbers with a Set:



function hasConsecutiveNums(array, N) {
  if (array.length &lt; N) return false;
  if (N &lt;= 1) return true;
  const sortedArray = [...new Set(array.slice().sort((a, b) =&gt; a - b))];
  let prev = sortedArray.shift();
  let count = 1; // first element of the array is already in prev
  for (const num of sortedArray) {
    if (num === prev + 1) {
      count++;
    } else {
      count = 1;
    }
    if (count === N) return true;
    prev = num;
  }

  return false;
}

console.log(hasConsecutiveNums([1, 4, 5, 6], 3)) // true
console.log(hasConsecutiveNums([1, 4, 5, 6], 4)) // false
console.log(hasConsecutiveNums([-1, 0, 1], 3)) // true
console.log(hasConsecutiveNums([1, 2, 2, 3], 3)) // true