If array contains one 1 after one 0 in order

  • A+
Category:Languages

I'm trying to test some arrays to check if they contain: at least one 0 at least one 1 at least one 1 after one 0

But my test of arr.includes(0,1) returns true for an array that should be false. They should be:

// var arr = [1,1,0,0,1,0,1] - should return true // var arr2 = [1,1,0,0] - should return false 

How can I test whether an array contains at least one 1 after one 0? thanks for any help

My code:

if(arr.includes(0) && arr.includes(1) && ___________) {     console.log(true); } else {     console.log(false); } 

 


You can join() the array items to check whether that includes() 01 or not:

var arr1 = [1,1,0,0,1,0,1]; var arr2 = [1,1,0,0]; function checkData(arr){   if(arr.join('').includes('01')) {     return true;   } else {     return false;   } } console.log(checkData(arr1)); // true console.log(checkData(arr2)); // false

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: