Recently I came across some code that is supposed to match these arrays try as I might I could not get all the pieces of the puzzle together to make sense
validateSequence([1,2,3,4,5,6,7,8,9]) === true
validateSequence([1,2,3,4,5,8,7,8,9]) === false
validateSequence([2,4,6,8,10]) === true
validateSequence([0,2,4,6,8]) === true
validateSequence([1,3,5,7,9]) === true
validateSequence([1,2,4,8,16,32,64]) === false
validateSequence([0,1,1,2,3,5,8,13,21,34]) === false
Here is the code that would validate it:
function validateSequence(x) {
return !!x.reduce(function(a, v, i, arr){
return (arr.length - 1 == i) ? a : a + (arr[i+1] - v) + ',';
}, '').match(/^(\d+,)\1*$/);
}
for example what are the two !! before x.reduce and how does it work?