1

I'm trying to backwards engineer the array methods push, pull, shift, unshift, but I can't seem to figure out how to a) construct it b) call it. How could I do this? Here are the conditions:

returns an empty array object. this object should have the following methods: push(val) adds val to the end of the array pop() removes a value from the end and returns it unshift(val) adds val to the beginning of the array shift() removes a value from the beginning and returns it the goal of this problem is to reverse engineer what array methods are actually doing and return an object that has those methods

Here is what I initially thought it should look like.

function createArray() {
    //CODE HERE

  this.push = function Push(value){
                if(index >= 0){
                 Mainarray[index++]=value;}
               };

   this.pop = function (){
                if(index >= 0){
                  index--;
                 return Mainarray[index];
                }
                else{
                  // display message of Empty Array
                  console.log('Error: Item is not array');
                }
              };

   this.unshift = function(){return ;};
}
Robby_rob
  • 173
  • 10

4 Answers4

2

You could use prototypes — like this:

function YourArray() {
  this.arr = [];
  this.index = 0;
}

YourArray.prototype.push = function( value ) {
  this.arr[ this.index++ ] = value;
  return this;
}

var arr = new YourArray();

arr.push('foo');
spmurrayzzz
  • 126
  • 2
1
function NewArray() {
    this.array = [];
}; /* Class */

NewArray.prototype.push = function(data) {
    this.array.push(data);
} /* Method */
/* You should use prototypes, because all methods will became common, and if you are created methods like this.pop = function (){} then any instance will copy this functions */


var n = new NewArray();
n.push(2);
console.log(n);

Advantages of using prototype, vs defining methods straight in the constructor?

Community
  • 1
  • 1
0x860111
  • 396
  • 1
  • 3
  • 15
1

You can recreate the push method by assigning you array at position the length of the same array a value.

This is the prototype for the push:

Array.prototype.push = function(element) {
    this[this.length] = element;
};

and this is for the pop method:

Array.prototype.pop = function() {
    var key = this.stack.pop();
    var prop = this.object[key];
    delete this.object[key];
    return prop;
};

You can make your own methods by changing the prototype names. push to mypush or sthing

Example for your push function createArray:

this.push = function pushValue(value) {
    this.arr[this.arr.length] = value;
};
Iliyan Yotov
  • 81
  • 1
  • 5
0

I used native arrays methods as values assigned to keys in the returned object. The trick is to declare an array inside the object and use it as a reference. It should pass the checks you`re looking for.

function createArray() {
    //CODE HERE
    return {
       arr: [],
       push: function (val) {this.arr.push(val)},
       pop: function() {return this.arr.pop()},
       unshift: function (val) {return this.arr.unshift(val)},
       shift: function() {return this.arr.shift()}
    }
}
Max
  • 1