Last active
October 28, 2019 03:43
-
-
Save darmawan01/5ce8a9bc14eea819d8335a2bf4131c47 to your computer and use it in GitHub Desktop.
#jumathek6
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
class Brankas { | |
unlock(key) { | |
const keys = key.split('') | |
let turned = 0, | |
lastPosition = 0, | |
isForward = true; | |
for (let i in keys) { | |
// Key less than last position and is forward, turnaround backward | |
// Or key greater than last position and is turnaround before, turned again forward | |
if ((keys[i] < lastPosition && isForward && keys[i] != 0) || | |
(keys[i] > lastPosition && !isForward)) { | |
isForward = !isForward, turned += 1 | |
} | |
lastPosition = keys[i] | |
} | |
console.log(`${key} h#: ${turned}`) | |
} | |
} | |
new Brankas().unlock('187427') | |
new Brankas().unlock('21245') | |
new Brankas().unlock('1092848') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment