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"]
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.
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.
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.
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.
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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With