Basic JavaScript - Stand in Line

Tell us what’s happening:
Describe your issue in detail here.
I do not understand this part: when I code arr = arr.push(item) and console.log it, i get 6 instead of [5,6,7,8,9,1], can you explain that to me? Thank you so much.

   **Your code so far**
function nextInLine(arr, item) {
 // Only change code below this line
  arr = arr.push(item)
console.log(arr)
let firstElemRemoved = [arr].shift()
console.log(firstElemRemoved)
 return firstElemRemoved 
 // arr.push(item);
 // console.log(arr)
 // const removed = arr.shift();
 // console.log(removed)
 // return removed;


 // Only change code above this line
}

// Setup
const testArr = [5,6,7,8,9];

// Display code

console.log(nextInLine([5,6,7,8,9], 1));

   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Stand in Line

Link to the challenge:

What is the definition of the array’s push method? What does the method return? What are you doing with the returned value?

Hi, chiming in as well. You included box brackets and I think it’s confusing the entire function.

let firstElemRemoved = [arr].shift()

See how you put box brackets? arr was already defined as being an array, you don’t have to go in and add box brackets to make it an array, again. I hope that helps, somewhat - small typos like this really ‘confuse’ JavaScript!

1 Like

Hello, thanks so much for your help, but I was confused about this part: when I code this:
arr.push(item) and console.log (nextInLine([5,6,7,8,9], 1)), I get this arr [5,6,7,8,9,1]. But when I code this:arr = arr.push(item) and console.log(nextInLine([5,6,7,8,9], 1) I get 6. Can you explain it to me? Thank you?

1 Like

Hi, so it looks like you accidentally redefined the array.

Look at your function. Do you see this…?

arr = arr.push(item)

You’ve just redefined what that array/list is going to be in that entire function, and now that function is working with new values its expecting you to generate and define inside of the function, instead of running that list of numbers - the array we actually want to pass inside of the function - like we’re supposed to work with in this problem.

You do not at all need to redefine the array. You can let that array sit there with the .push method attached to it, and JavaScript will go on to use that array that the Free Code Camp users generated for the problem instead of getting mixed up with a new value or definition.

So the first line of your code should just be, what you have minus the ‘equals’ or the definition portion:

arr.push(item)

Now, you’re telling JavaScript to take the array/list, and use the push method on it, specifically using the item (the number, right? ) as the ‘thing’ you’re pushing onto the end of the array/list.

Try that and rework your problem from there, good luck!

2 Likes

This was very helpful, I was confused myself. Thank you for explaining!

Thank you for answering my question! I really appreciate your answer!!!

1 Like