JLuc01
JLuc01

Reputation: 187

Add a random div on html page with javascript (with no repeat)

Here is the code which does not work (Nothing is added). The first line will work if I remove the comment. I suppose the variable result is not taking a number or something else. I don't really understand why.

$(document).ready(function () 
    //document.getElementById(1).style.display = "inline-block";
    // https://www.youtube.com/watch?v=pqLS4oyJ8cA
    array.prototype.randsplice = function(){
        var rndNumber = Math.floor(Math.random()*this.length);
        var lstSplice = this.splice(rndNumber,1);
        return lstSplice;
    }
    var lstNumber = new Array(0,1,2,3,4,5,6,7,8,9);
    var result = lstNumber.randsplice();
    document.getElementById(result).style.display = "inline-block";
    var result = lstNumber.randsplice();
    document.getElementById(result).style.display = "inline-block";
    var result = lstNumber.randsplice();
    document.getElementById(result).style.display = "inline-block";
});

I tried to followed this interesting link: Randomly Empty a Javascript Array but without success. (obviously the style display is originally set to none for all div.)

Any help will be appreciated. Thanks

Upvotes: 1

Views: 151

Answers (2)

choz
choz

Reputation: 17868

You're almost there, I noticed 2 mistakes so far;

  1. You're missing { in $(document).ready(function (). Which should be $(document).ready(function () {
  2. array.prototype.randsplice should be Array.prototype.randsplice

And beware that you need jQuery to run this script. And here's the final fiddle

Upvotes: 2

Scimonster
Scimonster

Reputation: 33409

You need to set Array.prototype.randsplice, with a capital A.

Upvotes: 1

Related Questions