0

I have this code for a custom pop() method:

Array.prototype.pop = function(index) {
    if (typeof index === "undefined") {
        index = this.length - 1;
    }
    var rtn = this.slice()[index];
    this.remove(this[index]);
    return rtn;
};

It works perfectly when I input a paramater (e.g. [1,3,5].pop(1) returns 3 and removes it).
However, when I use it with no parameters (e.g. [1,3,5].pop()) it returns undefined and does not edit the array. I think it is to do with the fact that function overloading does not work with 0 parameters. Please can you help me find alternatives or a solution to this problem. Thanks.

3
  • how about putting some kind of check ? Commented May 30, 2018 at 14:25
  • 2
    Array.remove does not exists indeed. Commented May 30, 2018 at 14:33
  • @AnwarNairi i created that method earlier in the js file, did not include it, sorry. Commented May 30, 2018 at 14:40

3 Answers 3

2

If you want what I think you want (return the indexed value and remove it, or use the last value if no index), then this is what you want...

Array.prototype.pop = function(index) {
    if (typeof index === "undefined") {
        index = this.length - 1;
    }
    // remove an array starting at index, with a length of 1,
    // and return the first value
    return this.splice(index, 1)[0];
};

// pop value by index
var arr = [1, 3, 5];

console.log(arr.pop(1));
console.log(arr.toString());

// pop last value
var arr = [1, 3, 5];

console.log(arr.pop());
console.log(arr.toString());

I'd also recommend putting some sense-checking in there to stop errors if you try and pop a value with an invalid index.

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

3 Comments

I was just about to suggest that exact edit, much cleaner
To make it even more succinct, you can use a default parameter and just write Array.prototype.pop = function(index = this.length - 1) { return this.splice(index, 1)[0]; }
@MBJH welcome. The only browser that doesn't have that implemented yet is IE (edge is fine), so if you have any concern of support for users with older windows os (or older browsers in general) you'd only want to use default parameter syntax if you're running your code through a preprocessor like babel.
0

You might also want to use a ForEach to loop and create a new array to fill the element only if the key is not the one provided in your arguments.

Also note that you can simplify your first check with some default value. Take a look (view online):

Array.prototype.pop = function(key = this.length - 1) {
    let array = [];

    this.forEach(function(element, index) {
      if( index !== key ) {
        array.push(element);
      }
    });

    return array;
};

console.log([1,3,5].pop(1)); // [1, 5]
console.log([1,3,5].pop()); // [1, 3]

Needless to say that overriding existing prototypes is strongly discouraged, you should probably think of another fancy name like Array.prototype.eject...

Comments

0

You don't even need check the type of index, the problem is that if no index is provided then index doesn't exist and you're trying to pass an actual value to a non-existent variable. What i would do is first change:

if (typeof index === "undefined")

for

if(!index)

For the sake of clarity.

Then within the if block change index = this.length - 1; for var index = this.length - 1;

var does the trick, as any variable is accessible outside that scope if declared with var.

Comments

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.