0

I came across book Object Oriented Javascript by Stoyan Stefanov. There is one exercise as below.

Exercise: Imagine Array() doesn't exist and the array literal notation doesn't exist either. Create a constructor called MyArray() that behaves as close to Array() as possible.

I have managed this much but push is not working. Can anybody guide me here?

function MyArray() {
    var arg = arguments;
    this.toString = function () {
        var string = arg[0];
        for (var i = 1; i < arg.length; i++) {
            string += "," + arg[i];
        }
        return string;
    }
    this.length = arg.length;
    this.push = function(p) {
        var a = this.toString() + ",";
        arg[this.length] = p;
        return a.concat(p);
    }
    this.pop = function() {
        return arg[this.length - 1];
    }
    this.join = function (j) {
        var strJoin = arg[0];
        for (var i = 1; i < arg.length; i++) {
            strJoin += j + arg[i];
        }
        return strJoin;
    }
}

var a = new MyArray(1, 2, 3, "test");
console.log(a.toString()); // 1,2,3,test 
console.log(a.length); // 4
console.log(a.push('boo')); // should return 5
console.log(a.toString()); // should return 1,2,3,test,boo
console.log(a.pop()); // boo
console.log(a.join(',')); // 1,2,3,test
console.log(a.join(' isn\'t ')); // 1 isn't 2 isn't 3 isn't test

jsfiddle: http://jsfiddle.net/creativevilla/prf3s/

7
  • 1
    why are you stringifying this in push??? Commented Sep 16, 2013 at 9:57
  • 1
    Why are you using arguments directly? I believe what the author means here is to have you create a LinkedList or some similar data structure. Commented Sep 16, 2013 at 9:59
  • 1
    your pop is actually peek... In short, you keep neglecting to keep the length property updated. Commented Sep 16, 2013 at 10:01
  • possible duplicate of Emulate Array Objects Commented Sep 16, 2013 at 10:01
  • @JanDvorak: I am not sure how to do this. Can you guide me here Commented Sep 16, 2013 at 10:02

1 Answer 1

1

Here's your fixed implementations of push and pop:

this.push = function() {
    // edit - push needs to accept more than one new item.
    for (var i = 0; i < arguments.length; i++) {
        arg[arg.length++] = arguments[i];
    }
    return arg.length;
}
this.pop = function() {
    return arg[--arg.length];
}

Fiddle: http://jsfiddle.net/prf3s/2/

As an aside, I think using the arguments object (which is pretty array like) as the internal data store might be a bit cheating.

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

2 Comments

Thanks mate. Just the code i was looking for. Also what do you mean As an aside, I think using the arguments object (which is pretty array like) as the internal data store might be a bit cheating. I shouldn't use that?
Well you're basically bolting your own class on to the arguments object which already supports access by index and has a length property. Obviously, it's the simplest way to solve the problem and it doesn't actually violate the rules... So I guess it's fine.

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.