Skip to content

Instantly share code, notes, and snippets.

@jcunanan05
Created June 14, 2019 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcunanan05/fcc2f7742a39cde1ddb6b34026aca4ad to your computer and use it in GitHub Desktop.
Save jcunanan05/fcc2f7742a39cde1ddb6b34026aca4ad to your computer and use it in GitHub Desktop.
// 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