0

I was trying to solve that problem : http://exercism.io/exercises/javascript/robot-name/readme but I'm stuck on the last requirement. Let me copy the problem

Write a program that manages 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, 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 should not allow the use of the same name twice when avoidable. In some exercism language tracks there are tests to ensure that the same name is never used twice.

This is the test suite : http://exercism.io/exercises/javascript/robot-name. I manage all of the tests except the last ones (related to unique reset).

My code in revealing module pattern

// To avoid duplicates
nameDb = {};

function Robot () {
  var name = '';

  // Helpers
  var randomizeLetter = function() {
    var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var charToChose = randomizeNumber(1, 26);
    return chars[charToChose - 1];
  };

  var randomizeNumber = function(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  };

  var nameGenerator = function() {
    var min = 100;
    var max = 999;
    var output = '';

    output += randomizeLetter();
    output += randomizeLetter();
    output += randomizeNumber(min, max);
    return output;
  };

  // Called on reset
  var reset = function reset() {
    var candidat = nameGenerator();
    if (name != '') delete nameDb[this.name];
    name = '';

    if(candidat in nameDb) {
      reset();
      return;
    } else {
      nameDb[candidat] = true;
      this.name = candidat;
    }
  };

  // Called immediately
  var newRobot = function newRobot() {
    var candidat = nameGenerator();

    if(candidat in nameDb) {
      newRobot();
      return;
    } else {
      nameDb[candidat] = true;
      name += candidat;
    }
  }();

  return {
    name: name,
    reset: reset
  };
}

module.exports = Robot;

Which returns expected 9767 to equal 10001'

What I was trying to do is : when the object is first invoked it has a name property. When the reset is called the name property is set to 0 and the corresponding DB entry is deleted. Then when the name is called again I try give a new one with recursion (which must be unique to that object instance). As you can see it doesn't work because the test doesn't returns 10001 unique ids

Antonin Cezard
  • 2,011
  • 1
  • 17
  • 30
  • 1
    var reset = function() { this.name = 'new name'; } – dbugger Jul 01 '16 at 14:19
  • for some reason I thought it wasn't necessary.. but yeah I'm still stuck after that because I have to reinit the name.. Though I could update the name without this in the newRobot function ? – Antonin Cezard Jul 01 '16 at 14:26
  • 1
    You need to keep scope in mind with javascript, and it gets complicated when you do things like this. To solve your second problem, change your reset function to: function() { this.name = nameGenerator(); } and add nameGenerator to your initial return object: return { name: name, reset: reset, nameGenerator:nameGenerator } – dbugger Jul 01 '16 at 14:34
  • Possible duplicate of [Expose private variables in Revealing Module Pattern](http://stackoverflow.com/questions/9672084/expose-private-variables-in-revealing-module-pattern) – Slippery Pete Jul 01 '16 at 14:42
  • thanks dbugger, and I edited my question so it can't be a duplicate. As you can see a single reset is ok, bu I fail to create an unique id for each reset – Antonin Cezard Jul 01 '16 at 15:31
  • 2
    One problem is that your recursion is called like this: `reset();`, in which case you're obliterating the meaning of `this`, which you use in the reset function. You should either be using closures to avoid the `this` keyword, or else you need to make sure to always call `this.reset()`, etc. – Christopher Davies Jul 01 '16 at 15:49
  • thanks christopher davies it was indeed this.reset() in the recursion.. I'll be honest I'm not sure why as you can see my knowledge of scope and recursion is not good. I will read more about them – Antonin Cezard Jul 01 '16 at 15:55

0 Answers0