Sha256: 48672df92a1640a3d431d500390029a78eb9c73ad95762d4b133332f1c011bf0

Contents?: true

Size: 1.28 KB

Versions: 141

Compression:

Stored size: 1.28 KB

Contents

function randomLetter() {
  var letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

  return letters.charAt(Math.floor(Math.random() * letters.length));
}

function Robot() {
  'use strict';

  this._name = this.generateName();
}

Robot.usedNames = {};

Robot.prototype = {
  constructor: Robot,

  generateName: function () {
    // This awesome err msg will never see the light of day. ;_; Checking the
    // length was slowing down the program too much.
    //
    //     if (Object.keys(this.constructor.usedNames).length >= 676000) {
    //       throw new Error("All possible names have been taken. " +
    //                       "Our robots are taking over the world! : D");
    //     }

    var name = randomLetter().toUpperCase();
    name += randomLetter().toUpperCase();
    name += (Math.random() + '').substr(2, 3);

    if (this.constructor.usedNames[name]) {
      return this.generateName();
    }

    this.constructor.usedNames[name] = true;

    return name;
  },

  get name() { return this._name; },

  set name(newName) {
    if (!(/^[A-Z]{2}\d{3}$/).test(newName)) {
      throw new Error('Name must be 2 capital letters followed by 3 ints.');
    }

    this._name = newName;
  },

  reset: function () { this.name = this.generateName(); }
};

module.exports = Robot;

Version data entries

141 entries across 141 versions & 1 rubygems

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