Stand in Line HELP!

Stand in Line HELP!
0.0 0

#1

Tell us what’s happening:
Can someone explain to me when the correct code is written the way it is? I read the solution and slowly interpreted but i still don’t know get it. Also is the number that are used to test, are they arr #s? or are they items #s? Someone please explain T_T

Your code so far

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

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

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36.

Link to the challenge:


#2

do you understand what the goal is?

Do you know what the array methods .push() and .shift() do?

The removed variable that is returned is what is being tested. It should be the first number in the array that was provided to the function.


#3

You probably have already read a fair bit about some of this, but I’ll include as much as possible so that it’s a self-contained explanation.

push() is a special method available to an array, which takes an item and adds it to the end of an array, it works as follows:

const arr = [0, 1, 2];

arr.push(3);

console.log(arr) // Prints [0, 1, 2 3] to the console

It is worth noting, because it’s a common mistake, is to assign the output of push() to a variable, thinking that it’s an array:

const arr = [0, 1, 2];
const newArr = arr.push(3);

console.log(newArr); // Prints 4. *Not* an array!
console.log(arr); // Prints [0, 1, 2].

As you can see, the push() method actually returns the length of the new array, instead of the array itself. In addition, it is important (for later) to keep in mind that the push() method actually mutates the original array (arr in our example is changed).

shift() works by removing the first item in the array:

const arr = [0, 1, 2];

arr.shift();

console.log(arr); // [1, 2];

And the return value of the shift() method is the value of the item that has been removed:

const arr = [0, 1, 2];
const brr = ['Nya', 1, 2];

const a = arr.shift();
const b = brr.shift();

console.log(a); // Prints 0.
console.log(b); // Prints 'Nya'.

console.log(arr); // Prints [1, 2];
console.log(brr); // Prints [1, 2];

Now let’s have a look at the function that you posted, paying attention to the additional comments:

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

  // arr = [1, 2, 3, 4, 5], item = 6;

  arr.push(item); // This push 6 into arr

  // arr = [1, 2, 3, 4, 5, 6], item = 6;

  var removed = arr.shift(); // This removes the first item in arr **AND** assigns the return value (value removed) to the variable removed

  // arr = [2, 3, 4, 5, 6], item = 6, removed = 1

  return removed;  // Change this line
}

So when you call the function nextInLine on the array [1, 2, 3, 4, 5], testArr becomes [2, 3, 4, 5, 6] and 1 is returned, that is:

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

console.log(nextInLine([1, 2, 3, 4, 5]); // Prints 1
console.log(testArr); // Prints [2, 3, 4, 5, 6]

One other thing that is perhaps worth mentioning here is that testArr is passed into the function as a reference—therefore any changes that you make to arr inside the function, which is a reference to testArr declared outside the function, will change it.

I hope that helps! :smile:


#4

Hey Honmanyau!

Just saw this, but yea I finally got it! Although, I do have one question for you. At the beginning when you were demonstrating push( )
You wrote: console.log (newArr); //Prints 4.
I get it’s not an array, but could you explain how you got 4?
(My wild guess is push(3) = 3. 3+1=4??)


#5

I did include an explanation. :sweat_smile:

Just in case it wasn’t clear:

const arr = [0, 1, 2, 3];

arr.length; // 4

arr.push(4); // 5
arr.length; // 5
console.log(arr); // [0, 1, 2, 3, 4]

arr.push(5, 6, 7); // 8
arr.length; // 8
console.log(arr); // [0, 1, 2, 3, 4, 5, 6, 7]

const brr = Array.apply( null, { length: 100 } ); // Creates an array with 100 `undefined`s

brr.length; // 100

brr.push(101); // 101
brr.length; // 101

brr.push(undefined, null); // 103
brr.length; // 103

I hope that helps!


#6

Thanks honmanyau, for that last note about how testArr is passed into the function as a reference. It’s confusing because it seems like a brand new variable that doesn’t seem to directly reference nextInLine . I don’t totally get it, but just trying not to get hung up on that and moving on. Because of this, I found this challenge particularly difficult.


#7

Javascript always passes primitives (Boolean, null, undefined, String, and Number) using value, but passes objects (arrays, functions, objects) as reference.


#8

JM-Mendez
Super helpful info, much appreciated.