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?

asked May 25, 2016 at 20:07
1

1 Answer 1

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()

answered May 25, 2016 at 20:13
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.