简体   繁体   中英

How to push and pull multiple values in Javascript

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.

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM