Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save KristofferK/2e4acbac0543a9a9d7aaf80859d3c533 to your computer and use it in GitHub Desktop.

Select an option

Save KristofferK/2e4acbac0543a9a9d7aaf80859d3c533 to your computer and use it in GitHub Desktop.
Checks if a given array has multiple items of the same data type next to each other. In the test code it checks if a given array has three numbers cohesively. This time using array.reduce
const array1 = [1,2,'a',3,'b',4,5]
const array2 = [1,2,'a',3,'b',4,5,6,'k']
const checkIfArrayHasNValuesOfSameDataTypeCohesively = datatype => n => array => (array.reduce((acc, val) => {
if ((acc.n = typeof val != datatype ? 0 : acc.n + 1) >= n) acc.passed = true;
return acc;
}, {n: 0, passed: false})).passed;
console.log(checkIfArrayHasNValuesOfSameDataTypeCohesively('number')(3)(array1)); // false
console.log(checkIfArrayHasNValuesOfSameDataTypeCohesively('number')(3)(array2)); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment