nextInline array stuck

Tell us what’s happening:

I don’t think to understand how to go about this. I am completely stuck here. I will really appreciate your help, so that i can proceed with my lesson.

Your code so far
function nextInLine(arr, item) {
// Your code here
var arr = [1,2,3,4,5];
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));


function nextInLine(arr, item) {
  // Your code here
 var arr = [1,2,3,4,5];

 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));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/stand-in-line

This challenge is really asking you to perform 3 tasks.

  1. Add the number to the end of the array

  2. Remove the first element of the array

  3. Return the element that was removed

I would start with trying to accomplish the first task above. You have already learned an array method which will allow you to add an element to the array passed into the function. Try writing that code first.

Next, the code arr.shift() does remove the first element from arr, but that is it. You should review the previous challenge named Manipulate Arrays With shift(), because shift actually returns a value when called and you are not taking advantage of this to solve the 3rd task above.

Finally, your current code returns item, which is the same item that was passed into the array. See my last point above, which will help you get the correct value to return.

Regarding this, I’m confusing between these 2 different result but seem from the same process:

var arr = [1,2,3];
var removed = arr.shift();
console.log(removed); // the result is 1

but in the same thing if I don’t use the variable removed then it is:

var arr = [1,2,3];
arr.shift();
console.log(arr.shift()); // result is 2

so the thing I would understand here is the arr.shift() has removed the second element of the array.

thank you for any help.

Hi Tony…

I am relativly new to coding but as far as I understand console.log() is a function. That is why it “executes” the argument “arr.shift()” when entered directly between the parantheses.
Since “arr.shift()” allready has been executed one line before, which removed the “1” from the array the next in line is “2”.

as opposed to

when passing the variable “removed” as an argument, the console.log’s “execution-command” is to read the variable “removed”. without executing “arr.shift()” again.

note: don’t be confused by the word “execution-command” as I just made it up.