Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove random item from array and then remove it from array until array is empty

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"]
like image 824
Travis Michael Heller Avatar asked Mar 17 '16 19:03

Travis Michael Heller


People also ask

How do you remove random items from an array?

We are required to create a function removeRandom() that takes in the array and recursively removes one random item from the array and simultaneously printing it until the array contains items. This can be done through creating a random number using Math. random() and removing the item at that index using Array.

How do you remove an element from the end of an array?

To remove the last element from an array, call the pop() method on the array, e.g. arr. pop() . The pop method removes the last element from the array and returns it. The method mutates the original array, changing its length.

How do I remove an item from an array by value?

To remove an item from a given array by value, you need to get the index of that value by using the indexOf() function and then use the splice() function to remove the value from the array using its index.

How do you remove a value from an array in Java?

Removing Array Items By Value Using Splice If you know the value you want to remove from an array you can use the splice method. First you must identify the index of the target item. You then use the index as the start element and remove just one element.

How to remove a range of elements from an array?

The splice method can also be used to remove a range of elements from an array. If you know the value you want to remove from an array you can use the splice method. First you must identify the index of the target item. You then use the index as the start element and remove just one element. This is a simple example where the elements are integers.

How to remove random items from an array of literals?

We are given an array of string / number literals. We are required to create a function removeRandom () that takes in the array and recursively removes one random item from the array and simultaneously printing it until the array contains items.

How to delete items from the end of an array in JavaScript?

You can delete items from the end of an array using pop (), from the beginning using shift (), or from the middle using splice () functions. Let’s discuss them. The Array.prototype.shift () method is used to remove the last element from the array and returns that element:


1 Answers

The function, Array.prototype.pop() will remove an element from the last. So at this context, you have to use Array.prototype.splice(indext,cnt),

for(var i = array.length-1;i>=0;i--){
  array.splice(Math.floor(Math.random()*array.length), 1);
  console.log(array);
}

And since we are altering the array, we have to traverse it in reverse way, so that the index will not be collapsed.

like image 86
Rajaprabhu Aravindasamy Avatar answered Oct 11 '22 13:10

Rajaprabhu Aravindasamy