Sha256: 4ad470ac22de1a2ff390b9b9d5dff8c5a3a44a9117d77c1badd32825e787aaab

Contents?: true

Size: 1.98 KB

Versions: 99

Compression:

Stored size: 1.98 KB

Contents

var Robot = (function () {
  'use strict';

  var VALID_DIRECTIONS = ['north', 'east', 'south', 'west']
  var INSTRUCTION_KEYS = {
    A: 'advance',
    L: 'turnLeft',
    R: 'turnRight'
  }

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

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

  Robot.prototype.orient = function (direction) {
    if (VALID_DIRECTIONS.indexOf(direction) === -1) {
      throw new Error('Invalid Robot Bearing');
    }

    this.bearing = direction;
  };

  Robot.prototype.advance = function () {
    switch (this.bearing) {
      case 'north':
        this.coordinates[1]++;
        break;
      case 'east':
        this.coordinates[0]++;
        break;
      case 'south':
        this.coordinates[1]--;
        break;
      case 'west':
        this.coordinates[0]--;
        break;
    };
  };

  Robot.prototype.turnLeft = function () {
    var directionPosition = VALID_DIRECTIONS.indexOf(this.bearing);

    if (directionPosition > 0) {
      this.orient(VALID_DIRECTIONS[--directionPosition]);
    } else {
      this.orient(VALID_DIRECTIONS[VALID_DIRECTIONS.length - 1]);
    }
  };

  Robot.prototype.turnRight = function () {
    var directionPosition = VALID_DIRECTIONS.indexOf(this.bearing);

    if (directionPosition < (VALID_DIRECTIONS.length - 1)) {
      this.orient(VALID_DIRECTIONS[++directionPosition]);
    } else {
      this.orient(VALID_DIRECTIONS[0]);
    }
  };

  Robot.prototype.instructions = function (instructionKeys) {
    return instructionKeys.split('')
      .map(function (key) {
        return INSTRUCTION_KEYS[key];
      });
  };

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

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

  return Robot
})()

module.exports = Robot;

Version data entries

99 entries across 99 versions & 1 rubygems

Version Path
trackler-2.2.1.37 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.2.1.36 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.2.1.35 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.2.1.34 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.2.1.33 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.2.1.32 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.2.1.31 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.2.1.30 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.2.1.29 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.2.1.28 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.2.1.27 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.2.1.26 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.2.1.25 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.2.1.24 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.2.1.23 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.2.1.22 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.2.1.21 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.2.1.20 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.2.1.19 tracks/javascript/exercises/robot-simulator/example.js
trackler-2.2.1.18 tracks/javascript/exercises/robot-simulator/example.js