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.
The shift method does not take any arguments. It only removes the first element from the array on which it is called and returns it. It will return undefined if called on an empty array.
Walking through the code of your first example:
function test(para1, para2){
// In the line below para2 means nothing here.
// All that happens is the first element `1` is removed from the para1 array
para1.shift(para2); // after this line executes, para1 is [2, 3]
// The next line returns the result of pushing para2 (4) to the end of the para1 array.
// The push method returns the length of the array after the new item is added.
// It does not return the array itself.
return para1.push(para2); // after this line is executed para1 is [2, 3, 4] but returns the value 3
}
console.log(test(testArr, 4));
Walking through the code of your second example:
function test(para1, para2){
// In the line below para2 means nothing here.
// All that happens is the first element `1` is removed from the para1 array
para1.shift(para2); // after this line executes, para1 is [2, 3]
// The next line returns the result of pushing "nothing" to the para1 array.
// Nothing gets added to para1 so it still is [2, 3].
// The push method returns the length of the array after the new item is added.
// Since nothing was added, 2 (the length of the array) is returned.
return para1.push(); // after this line is executed para1 is [2, 3] but returns the value 2
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?
My guess is the creators of JavaScript determined if you are pushing a new element into an array, you already know what that value was you just pushed. The only useful information would be the length of the new array after pushing. For shift, if you are removing an element and care about what element was actually removed, it makes sense to have the method return that value so that you could use it in some way.
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.