DEV Community

Discussion on: Daily Challenge #5 - Ten Minute Walk

Collapse
 
jaloplo profile image
Jaime López

Here my contribution with javascript language:

function getWalkSteps(walkTime) {
  if(walkTime % 2 > 0) {
    throw new Error('You cannot come back');
  }

  const distance = walkTime/2;
  const x = Math.floor(Math.random(distance) * distance);
  const y = distance - x;

  return new Array(walkTime)
    .fill('e', 0, x)
    .fill('n', x, x+y)
    .fill('w', x+y, x+y+x)
    .fill('s', x+y+x, x+y+x+y);
}