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/4e70cc129a1d4da4e034ad1cc3bb5390 to your computer and use it in GitHub Desktop.
Save darmawan01/4e70cc129a1d4da4e034ad1cc3bb5390 to your computer and use it in GitHub Desktop.
#jumathek7
class Clock {
currentPosition(code) {
// Split the code to array of char
const codes = code.split('')
// Initiate some variables
let isMove = false,
position = 60,
currentCode = '';
// Iterate the codes
for (let c in codes) {
// decide if clock is start or not, with checking out if currentCode is equal with code in iteration
// and isMove equal to false.
if (codes[c] === currentCode && !isMove) {
// set isMove to true, mark the clock is started
isMove = true
// set the position
// if currentCode equal to i and last position is 60 reduce position.
// if not set position move to 1
position = (currentCode === 'i' && position === 60) ? 60 - 1 : 1;
// after starting continue the interation
continue
}
// check is a clock move
if (isMove) {
// check first if a clock is moving and the code not equal to
// currentCode, stop the clock
if (codes[c] !== currentCode) {
// set isMove to false
isMove = false
// then continue iteration
continue
}
// set the position
// if code equal to a
position = (codes[c] === 'a') ?
// and position is 60, move clock to 1 position. if not position, plus 1.
(position === 60) ? position = 1 : position += 1 :
// another condition if code not equal to a, and position is 1.
// move clock to 60 position, if not position minus 1
(position === 1) ? position = 60 : position -= 1;
}
// save the currentCode
currentCode = codes[c]
}
console.log(position);
}
}
// Expecting 60
new Clock().currentPosition('aiaiaiaiaiaiaiai')
// Expecting 21
new Clock().currentPosition('aaaaaaaaaaaaaaaaaaaaaa')
// Expecting 1
new Clock().currentPosition('aaiiaa')
// Expecting 1
new Clock().currentPosition('aaaiaiaiiiiaaai')
// Expecting 30
new Clock().currentPosition('iiiiiiiiiiiiiiiiiiiiiiiiiiiiiii')
// Expecting 30
new Clock().currentPosition('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment