Created
November 13, 2018 17:11
-
-
Save runandrerun/a0d737af2cc8eb9a1de8061f6e39b093 to your computer and use it in GitHub Desktop.
Combination Lock Problem Set
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
combinationLock = (initialState, code) => { | |
let totalTurns = 0; | |
initialState.forEach((i, index) => { | |
let turns = Math.abs(i - code[index]); | |
if (turns <= 5) { | |
return (totalTurns += turns); | |
} else { | |
const newTurns = 10 - turns; | |
return (totalTurns += newTurns); | |
} | |
}); | |
return totalTurns; | |
}; | |
combinationLock([0, 0, 0], [3, 1, 8]) | |
combinationLock([2, 3, 4, 5], [5, 4, 3, 2]) | |
combinationLock([1, 9, 1, 9], [0, 0, 0, 0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment