Created
June 14, 2019 23:19
-
-
Save jcunanan05/fcc2f7742a39cde1ddb6b34026aca4ad to your computer and use it in GitHub Desktop.
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
// Complete the jumpingOnClouds function below. | |
function jumpingOnClouds(cloudList) { | |
const SAFE = 0; | |
const DANGER = 1; | |
let stepCount = 0; | |
// loop through the array | |
for ( | |
let i = 0, | |
totalSteps = cloudList.length -1; | |
i < totalSteps; | |
i++ | |
) { | |
// edge case | |
if (i === totalSteps - 1 && cloudList[i] === SAFE) { | |
stepCount++; | |
i += 1; | |
} | |
// special case no 2nd step | |
else if (cloudList[i + 1] === SAFE && cloudList[i + 2] === undefined) { | |
stepCount++; | |
} | |
// skip 2 safe clouds | |
else if (cloudList[i + 1] === SAFE && cloudList[i + 2] === SAFE) { | |
// adjust pointer | |
i += 1; | |
stepCount++; | |
} | |
// 1st cloud is dangerous | |
else if (cloudList[i + 1] === DANGER && cloudList[i + 2] === SAFE) { | |
i += 1; | |
stepCount++; | |
} | |
// 2nd cloud is dangerous | |
else if (cloudList[i + 1] === SAFE && cloudList[i + 2] === DANGER) { | |
stepCount++; | |
} | |
} | |
return stepCount; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment