Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function randomize(array){
- let newArr=array.slice();//creates copy of array
- let arLength=newArr.length;//gets array length
- let newCollection=[]; //creates a new array to restore items into
- let numList=[];//creates a new array to document the iterated random items
- function genRandNum(){//generates a random number and if the number already exists inside of numList it runs again
- let randNum= Math.floor( Math.random() * arLength);
- if(numList.includes(randNum)){
- genRandNum();
- }
- return randNum;
- }
- for(let i=0; i< arLength; i++){
- let randomNum=genRandNum();//gets random number
- numList.push(numList);//adds random number to random number list
- let item= newArr.splice(randomNum,1)[0];//gets index value of the array past in
- newVideoCollection.push(item);//adds randomly pick item to the new array
- }
- return newVideoCollection; //returns the new randomized array
- }
- function genRandNum(){
- let randNum= Math.floor( Math.random() * arLength);
- if(numList.includes(randNum)){
- return genRandNum(); // Return here
- }
- return randNum;
- }
Add Comment
Please, Sign In to add comment