Created
February 10, 2018 21:53
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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