const { log } = console;
const { floor, random: rand } = Math;
/* --------------- helpers --------------- */
// ⭐️ random int: 0 ... n-1
// randomInt(5): choose from 0, 1, 2, 3, 4
function randomInt(n){
return floor( n * rand() );
}
// ⭐️ array.random()
Array.prototype.random = function(){
return this[randomInt(this.length)];
};
// ⭐️ string.random()
// ⭐️ Note: this may not work well with Unicode❗️
String.prototype.random = function(){
return this[randomInt(this.length)];
}
// ⭐️ string.shuffle()
String.prototype.shuffle = function(){
var a = this.split("");
var n = a.length;
for(var i = n - 1; i > 0; i--) {
var j = randomInt(i+1);
[a[i], a[j]] = [a[j], a[i]];
}
return a.join("");
}
// ⭐️ String.fromCharCodesBetween(65, 70) -> 'ABCDEF'
String.fromCharCodesBetween = function(from, to){
let array = [];
for(let i = from; i <= to; i++) array.push(i);
return array
.map(code => String.fromCharCode(code))
.join('');
};