Sha256: b857a8061ef77b3e5c5efb4248bbfbc4b7bf106f4a98efd5192f0c481054f676

Contents?: true

Size: 1.65 KB

Versions: 211

Compression:

Stored size: 1.65 KB

Contents

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

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

  orient(direction) {
    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

211 entries across 211 versions & 1 rubygems

Version Path
trackler-2.2.0.0 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.1.0.55 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.1.0.54 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.1.0.53 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.1.0.52 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.1.0.51 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.1.0.50 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.1.0.49 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.1.0.48 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.1.0.47 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.1.0.46 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.1.0.45 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.1.0.44 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.1.0.43 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.1.0.42 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.1.0.41 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.1.0.40 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.1.0.39 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.1.0.38 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.1.0.37 tracks/ecmascript/exercises/robot-simulator/example.js