Why can't I detect 3 odd integers in a row in an array?

Viewed 318

I want to check whether an array has three odd integers consecutively (meaning their array positions, not necessarily in order numerically). If there are then return true, else return false.

My code is:

var threeConsecutiveOdds = function(arr) {
    for(var i = 0;i <arr.length;i++){
        if(arr[i] % 2  !==0 && arr[i+1] % 2 !==0 && arr[i+2] % 2 !==0){
            return true;
        }else{
            return false;
        }
    }
}

I want to ask why it is showing false for threeConsecutiveOdds([1, 2, 34, 3, 4, 5, 7, 23, 12])

Where it should return true as there is three consecutive odd integers in this array [5,7,23] are three consecutive odds.

5 Answers

Your code only ever checks the first 3 elements in the array.

Instead of immediately returning after checking 3 elements, you need to return only when you've checked the whole array.

To do this, put the return false after the end of your loop, so that it only returns that after it's checked all the possible elements.

N.B. you don't actually need to check the last 2 array elements because there aren't enough numbers after them for the sequence to be possible.

Demo, testing both true and false results:

var threeConsecutiveOdds = function(arr) {

  for (var i = 0; i < arr.length-2; i++) {

    if (arr[i] % 2 !== 0 && arr[i + 1] % 2 !== 0 && arr[i + 2] % 2 !== 0) {
      return true;
    }
  }
  return false;
}

console.log(threeConsecutiveOdds([1, 2, 34, 3, 4, 5, 7, 23, 12]));
console.log(threeConsecutiveOdds([1, 2, 34, 3, 5, 4, 7, 23, 12]));

Change to this:

for(var i = 0; i <arr.length-2; i++) {
    if(arr[i] % 2  !==0 && arr[i+1] % 2 !==0 && arr[i+2] % 2 !==0){
        return true;
    }
}
return false;

There were two bugs. First, you were exiting as soon as you had checked the first three. The other one was reading past the end of the array, which is where -2 comes from.

There is no requirement of else statement, you can just write return false after for loop.

If three consecutive odd integers found, it will return true if not then it will complete the loop and then return the false flag.

var return_flag = function(array) {

  for (var i = 0; i < array.length-2; i++) {

    if (array[i] % 2 !== 0 && array[i + 1] % 2 !== 0 && array[i + 2] % 2 !== 0) {
      return true;
    }
  }
  return false;
}

console.info(return_flag([1, 2, 34, 3, 4, 5, 7, 23, 12]));

A little bit hacky but intuitive for checking three consecutive odds.

var threeConsecutiveOdds = (arr) => {
  return arr.map((n) => n % 2).join``.includes('111');
};

console.log(threeConsecutiveOdds([1, 2, 34, 3, 4, 5, 7, 23, 12]));

If you want to have some fun, you could also implement a recursive solution:

const arr1 = [1, 2, 3, 2, 5, 9, 7, 11, 6, 6, 8, 4];
const arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const arr3 = ['foo', 7.5, 3, {}, 5, 13, 7, 8, 9, 10, 11];
const arr4 = [1,3];

function checkOddSequence(arr, sequenceLength, counter = 0) {
  /* 
  * if the odd numbers counter is equal to the sequence
  * length we want, return true
  */
  if (counter === sequenceLength) return true;

  /*
  * if the array has less elements than the sequence
  * length we want, return false
  */
  if (arr.length < sequenceLength) return false;

  if (typeof arr[0] !== 'number' || !Number.isInteger(arr[0]) || arr[0] % 2 === 0) {
    /*
    * if the first element of the array is not a number
    * or is not an integer number or it is an even numbe,
    * reset the odd numbers counter
    */
    counter = 0;
  } else {
    // otherwise increment the odd numbers counter
    counter++;
  }

  /*
  * re-run the function, passing a copy of the array without the
  * first element
  * (passing a copy will prevent from modifying the original array)
  */
  return checkOddSequence(arr.slice(1), sequenceLength, counter);
}

console.log('arr1:', arr1.join(","),'\nhas arr1 3 consecutive odd numbers?', checkOddSequence(arr1, 3));
console.log('arr2:', arr2.join(","),'\nhas arr2 3 consecutive odd numbers?', checkOddSequence(arr2, 3));
console.log('arr3:', arr3.join(","),'\nhas arr3 3 consecutive odd numbers?', checkOddSequence(arr3, 3));
console.log('arr4:', arr4.join(","),'\nhas arr4 3 consecutive odd numbers?', checkOddSequence(arr4, 3));
console.log('arr1:', arr1.join(","),'\nhas arr1 4 consecutive odd numbers?', checkOddSequence(arr1, 4));
console.log('arr1:', arr1.join(","),'\nhas arr1 5 consecutive odd numbers?', checkOddSequence(arr1, 5));

Related