I have to generate a array of length 3 says [3,2,1].. by random from the array of length 4 says [1,2,3,4] using java script.
var ele=[1,2,3,4];
I have to generate a new array var three=new Array(3);
the value of the array "three" should be randomized without duplicate from array "ele" .
ex three=[3,2,4]...
Here is the code I tried,
var tempArray=[1,2,3,4];
var set_bf =[];
for(var k=0;k<tempArray.length;k++){
var rand=(Math.round((Math.random()*tempArray.length)));
set_bf.push(rand);
console.log('male :'+rand);
var idx = tempArray.indexOf(rand);
if(idx!=-1) tempArray.splice(idx, 1);
}