I have an array: A = [ 2, 2, 0, 0, -1, 1, -1, -1, -1 ]
I want to be able to return true in instances where 2 or more consecutive numbers are the same. So in this array, the output array should have 5 trues with [2,2], [0,0], [-1,-1,-1], [-1,-1] and [-1,-1].
So far I have used slice and map on the array through 2 consecutive numbers and have gotten 4 trues.
const solution = (A) => {
let compare = A.slice(1).map((n,i) => {
return (n === A[i])
})
console.log(compare) // => [ true, false, true, false, false, false, true, true ]
}
const A = [ 2, 2, 0, 0, -1, 1, -1, -1, -1 ];
solution(A);
But getting that fifth true on the [-1,-1,-1] is eluding me.
Currently the compare output I have is only going through 2 consecutive numbers which is why it's given me 4 trues. My question is basically how to go about checking for the 3 or more consecutive numbers.
My final would be something like
compare.filter(word => word === true).length
to get 5.