I'm trying to set values in an array (ansArr), in a random order. The positions are in the pos array, and each position that comes up in the random number is deleted from pos. For some reason, it's not working. I'm quite new to JS, hope this code makes any sense...
var _ans1="ANS1"
var _ans2="ANS2"
var _ans3="ANS3"
var _ans4="ANS4"
var pos = [0,1,2,3]; // array of positions
var ansArr = [_ans4, _ans4, _ans4, _ans4]; // _ans1-_ans3 will run over 3 of the _ans4's
var rand = Math.floor(Math.random() * 4); // 0-3
posTMP = pos[rand];
ansArr[posTMP]= _ans1;
pos.splice(pos[rand,1]); // delete the occupied position
rand = Math.floor(Math.random() * 3); // 0-2
posTMP = pos[rand];
ansArr[posTMP]= _ans2;
pos.splice(pos[rand,1]); // delete the occupied position
rand = Math.floor(Math.random() * 2); // 0-1
posTMP = pos[rand];
ansArr[posTMP]= _ans3;
alert (ansArr[0],ansArr[1],ansArr[2],ansArr[3]);
Thank you very much :)