What code is better?

I am learning JS, in the Stand in Line this was my solution:

function nextInLine(arr, item) {
  // Your code here
  var c = arr[0];
  for(i = 0; i < arr.length; i++){
    arr[i] = arr[i+1];
  }
  arr[arr.length-1] = item;
  
  return c;  // Change this line
}

or this code

function queue(arr, item) {
  // Your code here
  arr.push(item);
  return arr.shift();;  // Change this line
}

When I did it a completily forgot about push and shift function.
Also, I don’t understand why if the array is empty, the functions returns 1. It is supossed to return the element that was deleted from the array.

function queue(arr, item) {
  // Your code here
  arr.push(item);
  return arr.shift(); // Change this line
}

is better because it uses native functions, is logical and easy to read/understand. It is also shorter which means less likely to contain errors.

You wonder why it returns 1 if arr is empty?
I’m sure you can find the answer yourself! It’s a good exercise.
If you call queue([], 1), what does the code do?

ps: damn, just saw this was posted on oct 16! Sorry!

1 Like