I am trying to remove a random item from an array until the array is empty using jquery or javascript. I need to console out each time the random item. Basically i am going to create an element with a random image from the given array until all images have been created.
Here is my attempt for getting the random item and removing from array but it it does not go through the entire array-I am stumped.
"load": function(){
var imgArray = ['brain', 'mitochondria', 'microsope', 'beaker', 'beaker-2', 'scientist', 'cell', 'atom', 'dropper'];
function randomItem(array){
var arrayLength = array.length+1;
console.log(arrayLength);
for(var i = 0;i<array.length;i++){
var item = array[Math.floor(Math.random()*array.length)];
array.pop(item);
console.log(array);
}
}
randomItem(imgArray);
},
Here is my console output:
10
home.js:12 ["brain", "mitochondria", "microsope", "beaker", "beaker-2", "scientist", "cell", "atom"]
home.js:12 ["brain", "mitochondria", "microsope", "beaker", "beaker-2", "scientist", "cell"]
home.js:12 ["brain", "mitochondria", "microsope", "beaker", "beaker-2", "scientist"]
home.js:12 ["brain", "mitochondria", "microsope", "beaker", "beaker-2"]
home.js:12 ["brain", "mitochondria", "microsope", "beaker"]