Stand In Line javaScript Exercise ! Stuck!

Stand In Line javaScript Exercise ! Stuck!
0

#41

Okay for godsakes I got it XD but take my advice do not look at the checklist.
I still do not know what that checklist is asking of me.
I guessed the answer while looking through this discussion.

I don’t know if I understand right so someone help me on that part
I will show you what I understand

arr = 12345
item = 6

1st objective
put “item” into “arr”

2nd objective
remove 1 from arr

3rd objective
output removed number

Forgive me I do not have the computer code lingo down.
You are talking to an amateur in terms of terminology.


#42

You are basically correct. Its just about .push() to add the item and .shift() to remove the first number on the list.

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


#43

I think one thing you might want to think about is how we store information in javascript. When we want to remember a piece of information or data, we do that with a variable (var).

So before you “shift” off the first part of the array, you want to write code that remembers that piece of data.

var rememberMe = arr.shift[0] --> Before I chop off a piece of the array, I need to remember what it is!

Then you can remember what the variable is later by returning it’s name:

return rememberMe;


#44

You are a genius! Seriously! Thank you for the explanation, becuase up to this point in the course, everything was going well and I could see the correlation between examples and exercises. It was not obvious exactly what was required, so your progressive hinting and explanations were very helpful indeed.

Again, thank you.


#45

arr.push(item);
var removed = arr.shift();
return removed;


#46

I know this thread already has different solutions, but thought I would share mine.

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

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

To explain the above code. What I’m doing is pushing the item to the end of the array. So if my function had the two parameters passed in nextInLine([5,6,7],10), it would push(10) to the end and shift(5) out. I end up with [6,7,10].


#47

@ChadKreutzer Thank you so much for your insightful post regarding the .push() and .shift(). I was lost with this challenge as well and your explanation has helped me understand what is going on. :slight_smile: You rock!!


#48

These explanations helped a lot. Thank you! I also was getting stumbled on accidently calling .shift() twice


#49

As the above, I was also getting tripped up by the calling .shift twice also. Thanks again everyone above who made posts! Would have taken me much longer to figure this out!


#50

Brilliant explanation! I feel like I’ve got that down… also mesquite looks awesome!


#51

Hello guys,
I’ve read your replays, but I have another question. Can you help me with an answer @ChadKreutzer or someone else, if you have the time of course.

So we have 2 versions of code:

My solution

function nextInLine(arr, item) {
  var newArr = arr.push(item);
  var result = newArr.shift();
  return result;  
}

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

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

Your solution

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

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

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

Now the question: My solution doesn’t work, but yours work. Why is that? Is it because I wanted to store the array in another variable, and if so, why?

Thanks in advance. This is really driving me crazy.


#52

I realized I too had the mistake of having the extra line of arr.shift(); not realizing that it was processing twice and giving me a different result. huge “duh” moment.


#53

Hello,

I’ve been stuck on this exercise and, while I’ve received help, I can’t pass it. I get the arr.push is not a function message. This is my code.

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

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

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

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

What am I doing wrong?


#54

Think about the array you’re trying to add to. testArr[2] refers to the item in the 2 position of testArr which is 3. 3 isn’t an array, it’s a number. You can’t push anything into a number. Not sure what your reasoning was for adding that [2]

This is pretty basic stuff. You’re going to run into problems later if you don’t have this down. If I were you I’d spend some time doing challenges on Codewars.com until you feel comfortable 6 kata challenges with ease. This will make the FCC challenges a lot less painful (so much so that they’re fun!).

You can PM if you get stuck on codewars.


#55

Thank you, everyone, for helping me with this confusing block of code. I saw so close to pulling my hair out before I sought help. lol. :confounded: :sunglasses::joy:


#56

Thank you for making sense out of this, Chad! You are the man :grinning:


#57

Thanks that was an amazing breakdown!


#58

Thanks man, this was super helpful


#59

I need to buy a notebook and write everything down
Thanks for the help. It was so obvious now that I look at it…


#60

The trick is in exactly doing as the statement tells you to. Try to economize lines. Just try to comprehend what is asked of you and lay that down, and you can shift into full gear in no time!

See what I did there? :wink: