3

I've attempted to define a randomize method to Array.prototype like so :

Array.prototype.randomize = function() { // Yes, this method does cause the array to have 'holes', but its good enough for this example/demonstration
    var r = new Array(this.length); // Possible but somewhat unwanted to randomize the array in its place, therefore it has to be set to this new array later on.

    this.forEach(function(e) {
        r[Math.floor(Math.random() * r.length)] = e;
    });

    // how do I set 'this' to the variable r? (In order to change the array to the new and randomized array 'r')
    return r;

}

This method does return the randomized array, but how do I change the array itself as well?

12
  • 1
    you can't change this Commented Oct 20, 2016 at 22:22
  • 1
    What if Math.floor(Math.random() * r.length) is the same for each e? Commented Oct 20, 2016 at 22:24
  • 4
    You need to mutate the original array. Learn about in-place shuffling algorithms. Commented Oct 20, 2016 at 22:24
  • 2
    something else - your function doesn't shuffle the array anyway, you could end up with many "holes" in r Commented Oct 20, 2016 at 22:24
  • 1
    @WEB_UI You can change the values inside of the current array. this[i] = x. Commented Oct 20, 2016 at 22:25

2 Answers 2

3

As the comments say, changing an array in place in place is a better way to shuffle.

But if you did need to replace all of the elements in one go, you could use Array#splice:

Array.prototype.randomize = function() { 
    var r = /* the algorithm to get a replacement array... */;

    this.splice(0, r.length, ...r);
    return this;
}

... is the spread operator. It's part of ES2015.

Spread operator compatibility table

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

1 Comment

Great! Problem solved (although I am looking forward to shuffle the array in its place later on).
3

not possible to randomize the array in its place

Wrong.

how do I set 'this' to the variable array 'r' in order to change the array to the new one?

That's impossible in JavaScript. You cannot overwrite an object, not via the this reference and not via a normal variable, you have to actually mutate its properties. If you want to overwrite the variable in which the array reference is stored, you need to explicitly assign to that variable; you cannot do it via a method (since JS does not allow pass-by-reference) and you can only overwrite this reference, not all variables that might contain it.

1 Comment

Possible but somewhat unwanted to randomize the array in its place, and that's because I just wanted to know how it is possible to change the this reference which I now realize is impossible because apparently "JS does not allow pass-by-reference." Which is of course, totally fine. Good point though!

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.