Javascript Array sample()
Array.prototype.sample = function() { return this[Math.floor(Math.random()*this.length)] }
Array.prototype.sample = function () { return this[Math.floor(Math.random() * this.length)] }
Array.prototype.sample = function(){ return this[Math.floor(Math.random()*this.length)]; }; var values = ['banana', 'apple' , 'Orange' , 'Coconut' , 'Grenade']; module.exports = function(){ module.get = function(){ var initTime = new Date().getTime(); while(true){ var now = new Date().getTime(); if(now > initTime + 1000) return values.sample(); }/*from w w w . jav a2s. c om*/ }; return module; }();
function inclusiverange(int1, int2){ rangeArray = []/*from www .java 2 s . c o m*/ for (var i = int1; i <= int2; ++i){ rangeArray.push(i); } return rangeArray; } var numbers = inclusiverange(1, 10); Array.prototype.sample = function() { return Math.floor(Math.random() * this.length); }; function guessNumber(number){ var sample = numbers.sample(); var guess = (number == sample) ? 'correct' : 'incorrect'; return console.log('your guess is ' + guess); }
Array.prototype.sample = function(){ return this[Math.floor(Math.random()*this.length)]; };