Skip to content

Instantly share code, notes, and snippets.

@darmawan01
Last active October 28, 2019 03:43
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 darmawan01/5ce8a9bc14eea819d8335a2bf4131c47 to your computer and use it in GitHub Desktop.
Save darmawan01/5ce8a9bc14eea819d8335a2bf4131c47 to your computer and use it in GitHub Desktop.
#jumathek6
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