Avatar of w1zeman1p

w1zeman1p's solution

to Robot Name in the JavaScript Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Manage robot factory settings.

When robots come off the factory floor, they have no name.

The first time you boot them up, a random name is generated in the format of two uppercase letters followed by three digits, such as RX837 or BC811.

Every once in a while we need to reset a robot to its factory settings, which means that their name gets wiped. The next time you ask, it will respond with a new random name.

The names must be random: they should not follow a predictable sequence. Random names means a risk of collisions. Your solution must ensure that every existing robot has a unique name.

Setup

Go through the setup instructions for JavaScript to install the necessary dependencies:

http://exercism.io/languages/javascript/installation

Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

npm install -g jasmine

Run the test suite from the exercise directory with:

jasmine robot-name.spec.js

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing xit to it.

Source

A debugging session with Paul Blackwell at gSchool. http://gschool.it

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

robot-name.spec.js

var Robot = require('./robot-name');

describe('Robot', function () {
  // NOTE: The 'beforeEach()' and 'afterEach()' act as setup/teardown for this
  // test suite. See more: http://jasmine.github.io/2.0/introduction.html
  var robot;

  beforeEach(function () {
    robot = new Robot();
  });

  afterEach(function () {
    robot = null;
  });

  it('has a name', function () {
    expect(robot.name).toMatch(/^[A-Z]{2}\d{3}$/);
  });

  xit('name is the same each time', function () {
    expect(robot.name).toEqual(robot.name);
  });

  xit('different robots have different names', function () {
    var differentRobot = new Robot();
    expect(differentRobot.name).not.toEqual(robot.name);
  });

  xit('is able to reset the name', function () {
    var originalName = robot.name;
    robot.reset();
    var newName = robot.name;
    expect(newName).toMatch(/^[A-Z]{2}\d{3}$/);
    expect(originalName).not.toEqual(newName);
  });

  xit('should set a unique name after reset', function () {
    var i;
    var numResets = 10000;
    var usedNames = {};

    usedNames[robot.name] = true;

    for (i = 0; i < numResets; i++) {
      robot.reset();
      usedNames[robot.name] = true;
    }

    expect(Object.keys(usedNames).length).toEqual(numResets + 1);
  });

  // This test is optional.
  xit('there can be lots of robots with different names each', function () {
    var i;
    var numRobots = 10000;
    var usedNames = {};

    for (i = 0; i < numRobots; i++) {
      var newRobot = new Robot();
      usedNames[newRobot.name] = true;
    }

    expect(Object.keys(usedNames).length).toEqual(numRobots);
  });
});
'use strict';

function randomChar() {
  return ALPHABET[Math.floor(Math.random() * 25)];
}

function randomDigit() {
  return Math.floor(Math.random() * 9);
}

const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
const ALL = {};

class Robot {
  constructor() {
    this.reset();
  }

  reset() {
    let name = ''.concat(
      randomChar(), randomChar(), randomDigit(), randomDigit(), randomDigit()
    );
    if(!ALL.hasOwnProperty(name)) {
      ALL[name] = true;
      this.name = name;
    } else {
      this.reset();
    }
  }
}

module.exports = Robot;

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?