Here's my code:
var myArr = [1,2,3,4,5];
function queue(arr, item) {
return arr.push(item).shift();
}
I'm attempting to create a function queue which takes an "array" and an "item" as arguments. I need to
My code is not working. Can you help me figure this out?
Just don't chain the method calls:
function queue(arr, item) {
arr.push(item);
return arr.shift();
}
Or, if you want a single statement,
function queue(arr, item) {
return arr.push(item), arr.shift();
}
Alternatively, if you are mad enough, you could subclass Array and add a chainable push:
class MyArray extends Array {
chainablePush(item) {
this.push(item);
return this;
}
}
var myArr = new MyArray(1,2,3);
myArr.chainablePush(4).shift(); // 1
myArr; // MyArray [2,3,4];
because arr.push returns the length of the array, you can't chain the shift like that
simply do this
function queue(arr, item) {
arr.push(item);
return arr.shift();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With