Sha256: 5b73b138cec311691a09a0a6b8c7cf53b198813a695e9cf94f1188841a582dd9

Contents?: true

Size: 1.96 KB

Versions: 25

Compression:

Stored size: 1.96 KB

Contents

export class InvalidInputError extends Error {
  constructor(message) {
    super();
    this.message = message || 'Invalid Input';
  }
}

class Robot {
  constructor() {
    this.coordinates = [0, 0];
    this.bearing = 'north';
  }

  at(xcoord, ycoord) {
    this.coordinates = [xcoord, ycoord];
  }

  orient(direction) {
    const validDirections = ['north', 'south', 'east', 'west'];
    if (!validDirections.includes(direction)) {
      throw new InvalidInputError('Invalid Robot Bearing');
    }

    this.bearing = direction;
    return `The robot is pointed ${direction}`;
  }

  advance() {
    if (this.bearing === 'north') {
      this.coordinates[1] += 1;
    } else if (this.bearing === 'south') {
      this.coordinates[1] -= 1;
    } else if (this.bearing === 'east') {
      this.coordinates[0] += 1;
    } else if (this.bearing === 'west') {
      this.coordinates[0] -= 1;
    }
  }

  turnLeft() {
    if (this.bearing === 'north') {
      this.orient('west');
    } else if (this.bearing === 'south') {
      this.orient('east');
    } else if (this.bearing === 'east') {
      this.orient('north');
    } else if (this.bearing === 'west') {
      this.orient('south');
    }
  }

  turnRight() {
    if (this.bearing === 'north') {
      this.orient('east');
    } else if (this.bearing === 'south') {
      this.orient('west');
    } else if (this.bearing === 'east') {
      this.orient('south');
    } else if (this.bearing === 'west') {
      this.orient('north');
    }
  }

  instructions(s) {
    return [...s].map((character) => {
      if (character === 'L') {
        return 'turnLeft';
      } else if (character === 'R') {
        return 'turnRight';
      } else if (character === 'A') {
        return 'advance';
      }
    });
  }

  place(args) {
    this.coordinates = [args.x, args.y];
    this.bearing = args.direction;
  }

  evaluate(s) {
    this.instructions(s).forEach((instruction) => {
      this[instruction]();
    });
  }

}


export default Robot;

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.179 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.178 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.177 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.176 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.175 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.174 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.173 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.172 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.171 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.170 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.169 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.167 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.166 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.165 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.164 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.163 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.162 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.161 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.160 tracks/ecmascript/exercises/robot-simulator/example.js