[Solved] Javascript: Check to see if multiple array values are the same (order matters)


You can use a counter to see if values of 1 are sequential.

function containsSequence(arr, val, max) {
      var count = 0;

      for (var i = 0; i < arr.length; i++) {
        if (arr[i] === val) {
          counter++; // increment the counter by 1
        }
        else {
          counter = 0; // reset counter
        }
    
        // our counter met our maximum
        if (counter >= max) {
          return true;
        }
      }

      // if we get this far, there was no sequence
      return false;
    }
    
    console.log(containsSequence([0,0,1,1,1,0], 1, 3)); // true
    console.log(containsSequence([0,1,0,1,0,1], 1, 3)); // false

solved Javascript: Check to see if multiple array values are the same (order matters)