Basic JavaScript: Stand in Line - help required

Hello,

Currently having difficulties completing the lesson as I don’t fully understand how the setup actually works - I have read through the solution and also watched the video.

  1. I don’t understand the relationship between ‘var testArr = [1,2,3,4,5]’ and the function ‘function nextInLine(arr, item)’ - how does the function know to point to this array?
function nextInLine(arr, item) {
  // Only change code below this line
  
  return item;
  // Only change code above this line
  

}

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

// Display code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));

Hello,
The function nextInLine has two parameters an array arr and a data(can be a number, string or even an array ) item .
The testArr below the comment ‘//Setup’ is an array that will be passed into the function to test the code you enter below the comment ‘// Only change code below this line’.
On the second line below comment ‘// Display code’, the function is called with testArr and 6 so that wherever the parameters arr and item are used in your code , value of testArr which is [1,2,3,4,5] and 6 are used.
Hope this helps.

Regards,

Thank you for responding!

So am I right in saying that the function ‘nextInLine’ is already a reserved function that exists within Javascript? In other words, you don’t have to create the function for it to exist?

Also, the below code snippet is the line telling the nextInLine function add the number 6 to array sequence?

console.log(nextInLine(testArr, 6));

Thank you in advance.

Hi,
Happy to help.
The ‘nextInLine’ is not a reserved function within Javascript. It is a function that has been declared with two parameters, ‘arr’ and 'item.
The challenge is to complete it’s definition(i.e. the body or the code within the function, I’d like to think of it as the inner workings :)).
So in the context of the lesson ‘Queue’ , the definition may include an operation on the parameter ‘arr’ that pushes out its first item and pushes in the parameter ‘item’. The code depends on what the challenge is.
Do you follow?

Regards,

Yes that makes perfect sense!

So am I right in saying that the below code snippet gives the function the values to work and complete it’s job? As we’re telling the function to use the ‘testArr’ array and passing the number ‘6’ to add to the array?

console.log(nextInLine(testArr, 6));

Hi,
Yes you are right so long as the job to be completed has been defined within the function. For example, if I want ‘item’ to join the queue. I’d define a push operation on the ‘arr’ parameter as such ‘arr.push(item)’.

1 Like

Thank you for your help much appreciated!

1 Like

Thanks for the opportunity.

Stay blessed.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.