console.log('result', [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]
.reduce((acc, curr) => {
if (acc.count < 10) {
if (acc.prev === curr) acc.count++
else acc = {
prev: curr,
count: 1
}
}
return acc
}, {
prev: null,
count: 0,
}
).count >= 10 ? 'fail' : 'success'
)
rxjs
import { interval } from 'rxjs';
import { scan, mergeMap, takeWhile } from 'rxjs/operators';
interval(1000).pipe(
mergeMap(() => Promise.resolve(Math.round(Math.random() * 1))),
scan((acc, curr) => curr === 1 ? ++acc : 0, 0),
takeWhile(acc => {
console.log(acc)
return acc < 2
})
).subscribe()