Sha256: 1bed80818d8df821f247f6dd41ebc5567995d18ecc2b0a47e7112742d9a5ba3c

Contents?: true

Size: 1.98 KB

Versions: 54

Compression:

Stored size: 1.98 KB

Contents

'use strict';

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

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

Robot.prototype.orient = function (direction) {
  if (direction != 'north' &&
      direction != 'south' &&
      direction != 'east' &&
      direction != 'west') {
    throw 'Invalid Robot Bearing'
  }

  this.bearing = direction;
  return 'The robot is pointed ' + direction;
};

Robot.prototype.advance = function () {

  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;
  }
};

Robot.prototype.turnLeft = function () {

  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');
  }
};

Robot.prototype.turnRight = function () {

  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');
  }
};

Robot.prototype.instructions = function (s) {
  var result = [];
  s.split('').forEach(function (character) {
    if (character === 'L') {
      result.push('turnLeft');
    } else if (character === 'R') {
      result.push('turnRight');
    } else if (character === 'A') {
      result.push('advance');
    }
  });
  return result;
};

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

Robot.prototype.evaluate = function (s) {
  this.instructions(s).forEach(function (instruction) {
    this[instruction]();
  }, this);
};

module.exports = Robot;

Version data entries

54 entries across 54 versions & 1 rubygems

Version Path
trackler-2.1.0.0 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.0.8.55 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.0.8.54 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.0.8.53 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.0.8.52 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.0.8.51 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.0.8.50 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.0.8.49 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.0.8.48 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.0.8.47 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.0.8.46 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.0.8.45 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.0.8.44 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.0.8.43 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.0.8.42 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.0.8.41 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.0.8.40 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.0.8.39 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.0.8.38 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.0.8.37 tracks/javascript/exercises/robot-simulator/example.js