Sha256: 2b51c9bb765e9b6224111523ffad5a934b1416ac09142b38beb7ba8b646ce6b4

Contents?: true

Size: 1.65 KB

Versions: 160

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

160 entries across 160 versions & 1 rubygems

Version Path
trackler-2.2.1.154 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.153 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.152 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.151 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.150 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.149 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.148 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.147 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.146 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.145 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.144 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.143 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.142 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.141 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.140 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.139 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.138 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.137 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.136 tracks/ecmascript/exercises/robot-simulator/example.js
trackler-2.2.1.135 tracks/ecmascript/exercises/robot-simulator/example.js