I'm new to creating objects in JavaScript. I need to make a random number generator that doesn't repeat itself too often (I have not tried to implement that part in the code snippet below). How can I access n
from function RNG(n)
in RNG.prototype.rand
? It's showing up as unreachable in my editor the way I have it written now. I'm also not sure if I should return from RNG
or RNG...rand()
:
function RNG(n) {
this.n = n;
}
RNG.prototype.rand = function() {
var arr = [];
var num = Math.floor(Math.rand()*n);
//keep array of generated numbers
if(num < arr[0]){
arr.unshift(num);
}
else{
arr.push(num);
}
}