I do not understand stand in line

Tell us what’s happening:
Describe your issue in detail here.
I have managed to solve the challenge below by myself but it was after numerous attempts and even at that my understanding is still hazy. I am looking for pointers on where i can read more on this .Thanks in advance to those willing to help.

  **Your code so far**

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));
  **Your browser information:**

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

Challenge: Stand in Line

Link to the challenge:

1 Like

You didn’t include your actual solution so we can’t comment on what you did. Can you be a little more specific about what you don’t understand?

the challenge is very straight-forward on what you should do and in what order to achieve it, its hard to tell at which point you fail to understand it.
Are you well familiar how functions behave? Are you familiar with the return action in functions and how it works? Are you comfortable with manipulating arrays, push new elements at their end and remove existing ones, say from their start? Those are few things which are part of the challenge.
The code which is beneath the // Setup comment has the purpose to display on the console how the testArr is changed, after its being the subject of the nextLine function. In the current example, the function should add 6 on the array end and remove its first element(1). Hope that helps

1 Like

Hi @bbsmooth my solution was as follows :slight_smile:

function nextInLine(arr, item) {

  // Only change code below this line

  arr.push(item);

  var removed = arr.shift();

  return removed;

  // 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));
1 Like

I edited your post to include [spoiler] tags since it is a working solution.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Your solution is exactly right. To add or remove from the tail of an array, we push items on or pop items off. You did this exactly right.

To add or remove from the front of an array, we shift things on or unshift them off. And, when we use the unshift function, that returns (passes back) the thing we’ve removed. And you are catching the return value in a variable.

Array functions take some practice, but you’ve done well here!

1 Like

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