DEV Community

Discussion on: Daily Challenge #5 - Ten Minute Walk

Collapse
 
dimitrilahaye profile image
Dimitri Lahaye

During my reflexion about this challenge, I figured out that if input is an odd number, you will never be able to go back at your start position.

Here my JS implementation:

function direction(time) {
  if (time % 2 > 0) {
    throw 'You will never go back!!';
  }
  const ahead = Math.ceil(time/4),
    turn = Math.floor(time/4);
    return ('n'.repeat(ahead) + 'w'.repeat(turn) + 's'.repeat(ahead) + 'e'.repeat(turn)).split('');
}

// let's test it

try {
  console.log(direction(4)); // print ["n", "w", "s", "e"]
} catch (e) {
  console.log(e);
}

try {
  console.log(direction(1));
} catch (e) {
  console.log(e); // You will never go back!!
}

try {
  console.log(direction(3));
} catch (e) {
  console.log(e); // You will never go back!!
}