I am going to assume that you are asking how to get a NEW array made of three elements in your current array.
If you don'd mind the possibly of duplicates, you can do something simple like: getThree
below.
However, if you don't want values duplicated, you can use the getUnique
.
var arrayNum = ['One', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
function getThree() {
return [
arrayNum[Math.floor(Math.random() * arrayNum.length)],
arrayNum[Math.floor(Math.random() * arrayNum.length)],
arrayNum[Math.floor(Math.random() * arrayNum.length)]
];
}
function getUnique(count) {
// Make a copy of the array
var tmp = arrayNum.slice(arrayNum);
var ret = [];
for (var i = 0; i < count; i++) {
var index = Math.floor(Math.random() * tmp.length);
var removed = tmp.splice(index, 1);
// Since we are only removing one element
ret.push(removed[0]);
}
return ret;
}
console.log(getThree());
console.log("---");
console.log(getUnique(3));