I have an array like
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
I want to grab push a random number of these entries in order and push them in a new array up to a limit.
i.e. so for example if I entered (5) - it would order random entries to a new array like
[1, 4, 7, 10, 12]
I tried
var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
var newArr = [], num, roll;
//remove number from array
for(var i =0; i < arr.length; i++) {
num = Math.floor(Math.random() * arr.length);
newArr.push(arr[num]);
roll = arr.splice(num, 1);
}
But it doesn't really return what I need as I need the order to remain. I am using underscore if that helps ?