2

I'm pretty new to coding and am running into a roadblock. I'm working on a challenge that I just can't seem to figure out.

function createArray() {
  var array = [];
   array.push = function(val){
     array[array.length] = val;
     return array;
   };
   array.pop = function(){
     return array[array.length - 1];
  };

 return array;
 };

var myArray = createArray();

When I run the test to complete the challenge, this code seems to push and pop a single value, but it doesn't seem to push and pop multiple values(which is one of the parameters for completing the challenge). Does anyone have any ideas? Possible solution? Any help would be amazing.

2
  • 1
    The pop method need to return the last value and remove it from the array Commented Apr 23, 2016 at 3:40
  • Your implementation of pop just returns the last value of the array. Pop actually removes the last value of the array and returns it. Commented Apr 23, 2016 at 3:43

1 Answer 1

1

If you can use Array methods of course, but I thing you can, because you used array.length

array.pop = function () {
    return array.splice(array.length - 1, 1)[0];
}

array.push = function(value) {
    return array.concat(value)
}
Sign up to request clarification or add additional context in comments.

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.