1

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

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. The missing { was actually a copy paste error, but I really forgotten the uppercase A. I knew about JQuery. It is working perfectly now. Thanks again.
1

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

1 Comment

As in my comment above, the mistake was the uppercase A. Thanks.

Your Answer

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.