Absolutely Confused: Stand In Line

Absolutely Confused: Stand In Line
0.0 0

#1

Hello fellow campers!

So far I have been having a blast learning the fundamentals of web development here on Free Code Camp as a newbie. It’s been tough as FFC is known to not hold your hand, essentially forcing you to think. However, I seem to have met my match with this relatively basic assignment.

I have spent upwards of 4 hours trying to solve this assignment; primarily because the wording of the assignment was perceived as vague and esoteric at best. I simply didn’t comprehend what was being asked of me and understood even less of what I was EXPECTING to achieve via the checklist requirements. In those hours I DID manage THIS solution:

function nextInLine(arr, item) {
arr = testArr;
item = testArr.shift();
testArr.push(item);
return item;

But it does not satisfy the entirety of the checklist requirements. Finally I decided to look up the answer on here with the intention of studying as to why it is the correct code. I’m STILL lost. I don’t know WHY it is correct and I still don’t see how it all relates. The console.logs confused me too. I just wish there was slightly more context. It feels like I am simply missing something and I can’t pinpoint why.

If any of you guys have time, would you please explain this assignment and WHY it is correct in a more laymen style that even a two-year-old could understand? I appreciate any input as I know you’re all busy.


#2

the only difference I see is in your solution if the array taken is empty you trigger an error, and in the correct answer if the array taken is empty return the item taken

non English native btw


#3

Let’s compare your solution to what was asked of you in the challenge instructions.

function nextInLine(arr,item) {
  arr = testArr;
  item = testArr.shift();
  testArr.push(item);
  return item;
}

// 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 function is called in the 2nd to last line of code at the bottom with:

nextInLine(testArr, 6)

When the function is called with the above parameters, the function receives these values as arguments in the function. So testArr comes into the function as arr and the value 6 comes into the function as item.

The first task the challenge asks of you is “Add the number to the end of the array”. You have already learned how to do this in the previous challenge called Manipulate Arrays With push. You should review this again. To add the number (argument called item) to the end of the array, you would use the push function as follows:

arr.push(item);

Why? Because arr is really just a reference to testArr which was passed into the function and we want to push item (which contains the value 6) onto the end of this array.

The next part of the challenge is in two sentences, but can be accomplished with one or two lines of code. The task is “remove the first element of array. The nextInLine function should then return the element that was removed.” I will break this up into two lines of code and then show how it can be accomplished in one line of code. To remove the first element of the array, we can use a function which you should have learned in a previous challenge called Manipulate Arrays With shift. You should review this challenge. The shift functions removes the first element of an array and returns the element’s value, so we can accomplish this second required task with the following.

Two lines:

var elemToReturn = arr.shift(); // removes 1st element and assigns it to new variable
return elemToReturn; // returns the new variable value;

One line:

return arr.shift(); // removes 1st element and returns it's value

OK, that is what you could have done to solve the challenge. Now, let’s see what your solution does. See comments below each line of your code:

function nextInLine(arr,item) {
  arr = testArr;
  // not really needed as arr is already testArr
  
  item = testArr.shift();
  // you reassign item (which was the number 6) to  the first element
  // of testArr which is the number 1
 
 testArr.push(item);
  // you now push item (which is now the number 1) to the end of testArr
  
  return item;
  // you return the number 1 which is the value of item
}

#4

Hey man I just wanted to than you for taking the time to explain that to me. I guess a major factor was that the variable and the console.logs may have confused me, thus causing me to overcomplicate what was actually a very simple challenge. Thank you sir. I understand this clearly now.