Stand in line - Have I fluked it?

Essentially I said to myself I wasn’t going to sleep until I passed this unit, I wrote me some psuedo-code, re-sat the previous modules and wrote the below code that I used to pass the unit.

I was happy, elated, thrilled… but then it dawned on me that I didn’t actually amend the return statement like it tells me to do… so I’m hoping to get some clarificaition on what gives without being given the answer.

Is it wrong?

Hints are well appreciated.


function nextInLine(arr, item) {
// Your code here

item = arr.push(item);
item = arr.shift();

return item; // Change this line

// Test Setup
var testArr = [1,2,3,4,5];

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));

This works fine, but yes, I would say you’ve fluked it slightly, though the reason why this is the case is quite subtle. You’ve understood what everything does. But the push and shift methods both have side-effects. They do something in addition to resolving to a value. push adds a value to the end of an array and returns the new length of the array after that value has been appended. shift removes a value from the beginning of an array and returns that value.

So, say arr is [1,2,3] and item is 10:

// item is 10
item = arr.push(item)
// so
item = [1,2,3].push(10)
// so this puts 10 on the end of the array,
// so the new length of arr after that is 4
item = 4
// so
item = arr.shift()
// so
item = [1,2,3,10].shift()
// so arr is now [2,3,10] and
// item is the value you shifted
item = 1
// now return that
return 1

As I said, this works absolutely fine. But you don’t need to reassign item on the first line (you don’t actually need to reassign item at all – you can just return arr.shift()). Hopefully the above walkthrough makes it clear why.

There’s nothing wrong with the code per se, it’s just that you assigning the value of the push operation to item, which is unnecessary, indicates you’re not 100% on what these operations do.


At first, I didn’t understand the response.

But I think this comes down to the fact that somewhere along the way I’ve picked up a bad habit that the return keyword should only pass a variable, which (in my mind anyway) explains the creation/passing of the item variable throughout the function, however, setting the return keyword to arr.shift(); to return the value makes absolute sense.

Thanks @DanCouper, my understanding has improved… hopefully.

1 Like

Yeah, I totally understand your thinking here. What it does in this case though is obscure the meaning of the code somewhat – by [re]assigning that variable (item), it indicates that you’re using it, when in fact you aren’t. And in other situations the habit can quite easily cause problems, because you end up with a value or values assigned to a variable that shouldn’t really be assigned: you can quite easily lose track of the state of your program and introduce bugs.