Gist Quickie! Random Array Constructor in JS
Create randomized arrays for testing
function RandomArray(opts = {}) {
this.alphabet = 'abcdefghijklmnopqrstuvwxyz';
this.type = opts.type || 'number';
this.length = opts.length || 100;
this.max = opts.max || 100;
this.min = opts.min || 0;
this.getRandomEntry = () => {
switch(this.type.toLowerCase()) {
case 'letters':
return this.alphabet.charAt(Math.floor(Math.random() * this.alphabet.length));
case 'number':
default:
return Math.floor(Math.random() * (this.max - this.min) + this.min);
}
}
return Array.from({length: this.length}, this.getRandomEntry.bind(this));
}
const testA = new RandomArray();
console.log(testA); // [55, 5, 43, 56, 28, ... ]
const testB = new RandomArray({length: 50, max: 23, min: 10});
console.log(testB); // [18, 17, 10, 18 ... ]
const testC = new RandomArray({lenght: 30, type: 'letters'});
console.log(testC); //["k", "l", "f", "p", "r", ... ]