1

I have the following task - there is an array of some items. I use random, get item number, write item's value in div and delete element with item number from array. e.g.

array = (1,2,3) 
random = 2
div = 3
delete from array = (1,2)
looping, etc

So I wrote the following script:

 window.onload = function () {
 var verb_array = new Array (1,2,3)
 var random_verb_array = Math.floor(Math.random() * (verb_array.length - 0+0)) + 0;
 var random_verb_array_2 = verb_array[random_verb_array];
 var show = document.getElementById("wuza").innerHTML = random_verb_array_2;
 delete verb_array[random_verb_array];
 var test = document.getElementById("huza").innerHTML = '<Br><Br>' + verb_array
 }
<!DOCTYPE html> 

<div id="wuza"></div>
<div id="huza"></div>
Item has no value in array, but it's position still remains. How can I avoid it and finally total wipe it from the array?
Cove
  • 655
  • 3
  • 7
  • 17
  • Refer [http://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascript](http://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascript) – Garvit Mangal May 25 '16 at 20:11

1 Answers1

5

You're probably looking to use splice.

verb_array.splice(random_verb_array, 1);

So assuming random_verb_array is the random index you've come up with, it'll remove 1 (second parameter) from the given index (first parameter).

Learn more about Array.splice()

seminull
  • 121
  • 5