find consecutives

var findMaxConsecutiveOnes = function (arr, number) {
   //check for boundries
   if(!number || !arr.length) return;

  // maximum number of consectuives
  let max = 0;

  // count homy many consecutives before it ends (why it's 1 ? because a lonely number is still counted)
  let counter = 1;

  // you can ignore the next 2 variable if you want to use for loop instead of while
  let length = arr.length;
  let i = 1; // counting from index 1 because we are checking against index-1
  while (i < length) {

    if (arr[i] == arr[i - 1]) {

      // boom, we have a consecutive, count it now
      counter++;
    } else {

      // rest to 1
      counter = 1;
    }

    // always update the max variable to the new max value
    max = Math.max(counter, max);

    //for the sake of iteration
    i++;
  }
  return max== number;
};
console.log(findMaxConsecutiveOnes([5, 5, 5, 1, 1, 1, 1, 1]));

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source