Unclear why 'Stand in Line' answer works

Tell us what’s happening:

Hi,
First timer, stuck on this Stand in Line challenge.
The helpful hints suggest that the code below is the answer - but I don’t understand where arr is defined as being an array? - we just start pushing and shifting on it right away.
When did we say that it’s an array, containing five numbers?

Your code so far
function nextInLine(arr, item) {
// Your code here
arr.push();
var removed = item.shift();
return removed; // Change this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) 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

where arr is defined as being an array?

Here. This line means that the first parameter of the function ( arr) will be testArr:
nextInLine(testArr, 6)

In this line you call the function nextInLine and pass it as first argument testArr, which is defined few lines above:
var testArr = [1,2,3,4,5];
If you write something like
nextInLine(true, 6)
or
var testArr = {hello: ' I am an object};
the ‘program’ will crash ^^

EDIT:
I just saw that ‘Get a hint’ section, it’s awesome :open_mouth:

Thanks,
So testArr goes into the nextInLine function as the first parameter?
Cause that’s not their answer.

The rest of your explanation is skipping ahead - appreciate it, but I’m still working through the first issue.

1 Like

arr is defined to be an array by the statement of the problem. Referring back to the challenge you should notice that it said:
Write a function nextInLine which takes an array (arr) and a number (item) as arguments.

so by definition the arr variable must be an array.
And if you look further into the defined unit-tests listed they show things like:
nextInLine([], 5) should return a number.

and you can see here clearly that the first parameter of the nextInLine function is [] which is an empty array.
If you look at the other unit-tests you will see various arrays being passed in.

hope this helps.

1 Like

In js you don’t specify that arr is an array - if you pass something in that doesn’t support the correct operations it just crashes

I know, that sucks, but that’s part of the dynamica nature of js

Fwiw it’s a similar situation in python too, even though that has a stronger concept of types

It comes down from certain design decisions inherent in the language, there’s debates about strict and static typing to the point where there’s no unanimous answer to what’s best when designing a language

It’s an interesting thing to read up on if you’re interested, though for now just know there’s no straightforward way to guarantee it’s passed the correct type

Hi,

nextInLine(anArray, aNumber);  //your pass an array and a number to your function
// like this example
nextInLine([1,2,3,4,5] , 6);


function nextInLine(arr, item){  //(arr = [1,2,3,4,5], item = 6 )
  // do something here to put 'item' onto end of arr  [1,2,3,4,5,6]
  // do something here to remove the first element of arr  [2,3,4,5,6] 
  // return that first element that you removed - in this case a 1
}

With the provided example hard coded on the test page you should get
before array should be [1,2,3,4,5]
console output of 1
and afterwards your array should be [2,3,4,5,6]

Good luck