Guest User

Untitled

a guest
Nov 21st, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. function randomize(array){
  2. let newArr=array.slice();//creates copy of array
  3.  
  4. let arLength=newArr.length;//gets array length
  5.  
  6. let newCollection=[]; //creates a new array to restore items into
  7.  
  8. let numList=[];//creates a new array to document the iterated random items
  9.  
  10. function genRandNum(){//generates a random number and if the number already exists inside of numList it runs again
  11.  
  12. let randNum= Math.floor( Math.random() * arLength);
  13. if(numList.includes(randNum)){
  14. genRandNum();
  15. }
  16. return randNum;
  17. }
  18.  
  19. for(let i=0; i< arLength; i++){
  20. let randomNum=genRandNum();//gets random number
  21.  
  22. numList.push(numList);//adds random number to random number list
  23.  
  24. let item= newArr.splice(randomNum,1)[0];//gets index value of the array past in
  25.  
  26. newVideoCollection.push(item);//adds randomly pick item to the new array
  27. }
  28. return newVideoCollection; //returns the new randomized array
  29. }
  30.  
  31. function genRandNum(){
  32.  
  33. let randNum= Math.floor( Math.random() * arLength);
  34. if(numList.includes(randNum)){
  35. return genRandNum(); // Return here
  36. }
  37.  
  38. return randNum;
  39. }
Add Comment
Please, Sign In to add comment