match if 2 arrays have same values javascript code example
Example 1: check if array has same values javascript
const allEqual = arr => arr.every(v => v === arr[0]);
allEqual([1,1,1,1]);
Example 2: javascript check if an array has same consecutive values
const arr = ["g", "z", "z", "v" ,"b", "b", "b"];
const checkThree = arr => {
const prev = {
element: null,
count: 0
};
for(let i = 0; i < arr.length; i++){
const { count, element } = prev;
if(count === 2 && element === arr[i]){
return true;
};
prev.count = element === arr[i] ? count + 1 : count;
prev.element = arr[i];
};
return false;
};
console.log(checkThree(arr));
console.log(checkThree(["z", "g", "z", "z"]));