Created
March 27, 2018 18:48
-
-
Save misterussell/63db165b9caf0b8b14483789a5bcf0d3 to your computer and use it in GitHub Desktop.
Find the adjacent equal values in an arr of values
This file contains 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
// this function is rather long for doing something simple. | |
function getSustainedPeriods(arr) { | |
let life = []; | |
let lifeCount = 0; | |
let death = []; | |
let deathCount = 0; | |
for (var i = 0; i < arr.length; i++) { | |
if (arr[i] === 1) { | |
life[lifeCount] === undefined ? life[lifeCount] = [1] : life[lifeCount] = [...life[lifeCount], 1]; | |
} else lifeCount += 1; | |
if (arr[i] === 0 ) { | |
death[deathCount] === undefined ? death[deathCount] = [1] : death[deathCount] = [...death[deathCount], 1]; | |
} else deathCount += 1; | |
} | |
// remove empty values | |
life = life.filter(n => true) | |
.map(subArr => { | |
return subArr.reduce((a, b) => a + b); | |
}); | |
death = death.filter(n => true) | |
.map(subArr => { | |
return subArr.reduce((a, b) => a + b); | |
}); | |
return { life, death }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment