Stand In Line Alternative Testing

Hello!

Simply it took me a little while to fully grasp the Stand In Line question so I decided to spend even more time on it to try and make it intuitive/fall out of love with javascript.

So essentially I tried the excercise in reverse, shifting the array first before returning a push. However one interaction that I’m not certain of is when I input a value into the push function, the console.log returned a later value, almost as if shift had been executed twice, relative to only once if the there was no value in the push function. Code below:

function test(para1, para2){
    para1.shift(para2);
    return para1.push(para2);
}
var testArr = [1,2,3];
console.log(testArr);
console.log(test(testArr, 4));
console.log(testArr);

Returns

>[1, 2, 3]
>3
>[2, 3, 4]

Whilst

function test(para1, para2){
    para1.shift(para2);
    return para1.push();
}
var testArr = [1,2,3];
console.log(testArr);
console.log(test(testArr, 4));
console.log(testArr);

returns:

>[1, 2, 3]
>2
>[2, 3]

Any idea why this is the case? As far as I can see in both instances the shift function should only iterate once in both instances, but it appears to iterate twice in the first example.

Kind regards!

Thanks Randy, makes sense to me as far as how the code works and the results it turns up.

I guess my only question would be why it logs a length by default, relative to the normal instance in the course where the shift function results in the console logging the shifted number?

push returns the new length of the array, shift returns the removed element, they work differently

Perfect, thanks for explaining!

Perfect, thanks for the responses!

I was just curious to see if there was any logic I was missing regarding the interacting functions that led to different outcomes, but if its simply a matter of preference - well justified - then I’m fairly sanguine and can move on.

Thanks!