freeCodeCamp Challenge Guide: Stand in Line

freeCodeCamp Challenge Guide: Stand in Line
0

Stand in Line


Hints

Hint 1

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

Hint 2

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

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.


Solutions

Solution 1 (Click to Show/Hide)
function nextInLine(arr, item) {
  // Your code here
  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();.

Relevant Links

24 Likes

I came up with another solution that also works:

function nextInLine(arr, item) {
// Your code here
arr.push(item);

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

Is there a reason the solution you offer is preferable?

8 Likes

why has this wiki changed?

1 Like

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();.

function nextInLine(arr, item) {
// Your code here

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));

5 Likes

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.

4 Likes

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));

Answer is:

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

is all.

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

4 Likes

what do you think about this:

2 Likes

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

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

1 Like

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

10 Likes

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

  • console.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.

Additional resources:

Array Push: https://www.w3schools.com/jsref/jsref_push.asp
Array Shift: https://www.w3schools.com/jsref/jsref_shift.asp

1 Like

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

function nextInLine(arr, item) {
// Your code here

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));

I am completely lost :nerd_face: 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.

32 Likes

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;
}

2 Likes

Good, it really helped.Thanks man.

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.?

10 Likes

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.

2 Likes