Running some tests in Jasmine to try to get this code to work, discovered that ids were not unique. Which makes sense since they are generated randomly like so.
var Robot = function(){
this.name = makeid();
function makeid()
{
var text = "";
var possible ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for( var i=0; i < 2; i++ ){
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
var possibleNums ="0123456789";
for( var j=0; j < 3; j++ ){
text += possibleNums.charAt(Math.floor(Math.random() * possibleNums.length));
}
return text;
}
};
I need it to fulfill this test.
it('there can be lots of robots with different names each', function() {
var i,
numRobots = 10000,
usedNames = {};
for (i = 0; i < numRobots; i++) {
var newRobot = new Robot();
usedNames[newRobot.name] = true;
}
expect(Object.keys(usedNames).length).toEqual(numRobots);
});
I theorized that I might be able to make an array, push each name to it, and then compare for uniqueness. That looks like it might be frustrating. I'm wondering if there's another way, maybe to guarantee uniqueness at generation or some simple comparison tool not involving arrays.
Edit: The date stamp method would be a great way of ensuring unique ids but unfortunately I have to use the id generation method I used to pass another test. Essentially I need the id to be 5 chars with 2 capital letters followed by 3 numbers.