# freeCodeCamp Challenge Guide: Stand In Line

freeCodeCamp Challenge Guide: Stand In Line
0

#1

Remember to use `Read-Search-Ask` if you get stuck. Try to pair program and write your own code

### Problem Explanation:

In Computer Science a queue is an abstract data structure where items are kept in order. New items can be added at the back of the queue and old items are taken off from the front of the queue.

Write a function `nextInLine` which takes an array (arr) and a number (item) as arguments. Add the number to the end of the array, then remove the first element of array. The `nextInLine` function should then return the element that was removed.

• Change the code below `//Your Code here` and up to `//Change this line`.
• Ensure that you are editing the inside of the `nextInLine` function.
• Use an array function you learned to add the item to the end of the array arr.
• Use an array function you learned to remove the first element from array arr.
• Return the element removed.

## Hint: 1

The `push()` method adds an item to the end of an array.

try to solve the problem now

## Hint: 2

The `shift()` method removes the first element of an array. It also returns the element removed.

try to solve the problem now

## Hint: 3

The function `nextInLine` uses arr and item. Those are what the tests will use to pass the array elements they will test with. It allows the function to be reusable. Do not hardcode any of the tests inside the function.

try to solve the problem now

## Basic Code Solution:

``````function nextInLine(arr, item) {
arr.push(item);
var removed = arr.shift();
return removed;  // Change this line
}
``````

### Code Explanation:

• Push item at the end of arr.
• Call the `shift()` method on arr to get the first item and store it in removed.
• Return removed.

Example Run

• Test `nextInLine([2], 1);` runs.
• The `nextInLine` function is called. arr becomes [2]. item becomes 1.
• `arr.push(item);` Pushes 1 to [2]. So arr is now [2,1].
• `var removed = arr.shift();` removes the first element. So arr is now [1]. 2 has been removed and is stored in removed.
• `return removed;` 2 is returned.

Note: You don’t actually need the variable removed. The element removed can be returned directly using `return arr.shift();`.

## NOTES FOR CONTRIBUTIONS:

• DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
• Categorize the solution in one of the following categories — Basic, Intermediate and Advanced.

See `Wiki Challenge Solution Template` for reference.

Confused about Stand in Line challenge from JS
#2

I came up with another solution that also works:

function nextInLine(arr, item) {
arr.push(item);

return arr.shift(); // Change this line
}

Is there a reason the solution you offer is preferable?

#3

why has this wiki changed?

#4

if you look at solution above:

its mentioned
Note: You don’t actually need the variable removed. The element removed can be returned directly using return arr.shift();.

#6

function nextInLine(arr, item) {

arr.push(item);

var temp = arr.shift();

return temp; // Change this line
}

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

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 10)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));

#7

I solved this a different way, but I’m wondering if this way is acceptable or if it’s more logical to create a new variable within the function, and then return that variable (based on main solution above). I wanted to make sure because when I committed my code for the “Stand in Line” problem, it was accepted.

Here is my solution:

``````function nextInLine (arr, item) {
testArr.push(item);
testArr.shift(arr[0]);
return arr[0] || item; // used "or" operator instead of creating a new variable.
}
``````

So here, even if the arguments passed to the function are `([], 1)`, `1` will still be returned to fulfill these instructions from the problem:

`nextInLine([], 1) should return 1`

Thanks for any input.

EDIT:

I realize now that the solution I submitted does not fulfill the parameters of the outlined problem. For `nextInLine([5,6,7,8,9], 1)` should return `5`, the output actually show `6`. Not really sure why my code was accepted as a solution for this problem.

#8

This is my code:

function nextInLine(arr, item) {
arr.push (item);
var removedItem = arr.shift();
return removedItem; // Change this line
}

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

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 10)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));

#9

``````arr.push(item);
return arr.shift();
``````

is all.

Your code works because it returns the same values as the example.

#10

#11

You have a lot of extra code.
It’s enough two lines. See above

#12

for explanatory purpose, his code gives more insight on whats going on

#13

What the hell is JSON.stringify and how is this relevant to the exercise?

#14

I just coded a basic script for this challenge. Just a few reminders:

• c`onsole.log(nextInLine(testArr, X)); // Modify this line to test` Where X is the item that will be pushed by the `.push()`

• You can change the values of `testArr` - feel free to change the values. Doesn’t matter what elements are inside as long as you are able to execute the push, shift and return requirements.

#15

Hello campers,this is how I implemented the queue concept in the challenge:

function nextInLine(arr, item) {

arr.push(item);

var removed = arr.shift(item);

return removed; // Changed
}

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

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 10)); // Modified
console.log("After: " + JSON.stringify(testArr));

#16

I am completely lost The previous exercises on manipulating arrays show instances of arrays not parameters with no arguments passed into them. The only arrays I have seen look like this: `var ourArray = ["Stimpson", "J", "cat"];` So, I’m thinking I need to create my own. And there are no arguments (values) for the parameters. Then I’m thinking I need to stick an array or two in there so I’ll have something to .push and .shift.

“Add the number to the end of the array then remove the first element of the array”

What array? I don’t think we were really prepared for this exercise, judging from the previous lessons.

#17

I had a couple of different solutions which work. Have heard there are a many ways to solve code problems. Any one see any issues with my solutions below? Ended up using the second one to avoid confusion with the “item” argument and variable name.

code #1
function nextInLine(arr, item) {
arr.push(item);
item = arr.shift();
return item;
}

code #2
function nextInLine(arr, item) {
arr.push(item);
itemLost = arr.shift();
return itemLost;
}

#18

Good, it really helped.Thanks man.

#19

Seriously I don’t understand this question.

it has two arguments arr and item.

It says to add a number and remove the element, I got this point too PUSH and SHIFT.

What I don’t understand here is , How I relate arr to testArr and sync it up with item.

anyone help me with step by step flow.?

#20

Thanks Ivan. Now I get it, quite easy but I had to come here to understand what’s requested and how to achieve. The use of JSON here just confuses me. Thanks anyways… simple straight solution.