I'd like to practice oojs so I've written code:
function Randoms(c, l, h) {
var count = c,
min = l,
max = h,
nums = Array(),
self = this;
//returns random number from given max and min
var getRand = function () {
return Math.floor(Math.random() * (max - min) + min);
};
//checks if given number is unique
var check = function (a) {
for (var i = nums.length - 1; i >= 0; i--) {
if (nums[i] == a) {
return true;
}
}
return false;
};
//makes array with random, unique numbers
this.build = function () {
var r = getRand();
if (nums.length === 0) {
nums.push(r);
} else {
do {
r = getRand();
} while (check(r));
nums.push(r);
}
if (nums.length == count) {
return true;
} else {
this.build();
}
};
//returns array
this.get = function () {
return nums;
};
//simulating constructor
this.build();
}
var a = new Randoms(1, 1, 18);
console.log(a.get());
The purpose is to get array with random, unique numbers.
What do you think? Is it a good code or does it require modyfications?