John Smith
John Smith

Reputation: 13

Re-filling an array when it is null

Hi there I wonder if you could help.

I have a random number generator which when you press a button reduces the amount of numbers in the array ie. the one that got randomised has now been used and is removed.

var crds = Math.floor((math.random()*myArray.length));

myArray = ["2","4","6","8","10","100","20","Bingo!"];
Array.prototype.removeByIndex = function(index){ 
    this.splice(index,1)
}

I have not included the attachements to button presses but this code tells the story - When the button is pressed it splices the array via index of the number randomised. What I do not know how to do is to re-initialise the array when all the numbers have been removed.

if(myArray[crds]==null){ myArray = ["2","4","6","8","10","100","20","Bingo!"]; }

any help would be appreicated

Upvotes: 0

Views: 48

Answers (1)

Satpal
Satpal

Reputation: 133403

Try this

if (myArray.length == 0) {
    myArray = ["2", "4", "6", "8", "10", "100", "20", "Bingo!"];
}

Demo: http://jsfiddle.net/WkaV4/

Upvotes: 1

Related Questions