Created
November 21, 2018 09:27
-
-
Save c-j-j/19547b8c8b07efe1f3c6fa01d40480a2 to your computer and use it in GitHub Desktop.
Counting Valleys - Javascript
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 countingValleys function below. | |
function countingValleys(n, s) { | |
let level = 0; | |
let numberOfValleys = 0; | |
for (let i = 0; i < n; i++) { | |
const next = s[i] | |
if (next === 'U') { | |
if (level === -1) { | |
numberOfValleys = numberOfValleys + 1; | |
} | |
level = level + 1; | |
} else if (next === 'D') { | |
level = level - 1; | |
} | |
} | |
return numberOfValleys; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment