Stand In Line javaScript Exercise ! Stuck!

So perfect and clear explanation :slight_smile:

1 Like

Function nextInLine(arr, item) {
return arr.push(item) && are.shift();
}
That should solve the problem

We would rather you didn’t post full working solutions, instead try to help other campers understand how to solve it themselves. Your solution is different enough from the standard basic solution to make me think it’s reasonable to post it, but you need to explain why it works: it’s too complex and tricky a solution to just dump it there on its own without explanation (it requires knowledge of how to short-circuit boolean operators to understand what it’s doing)

2 Likes

Continuing the discussion from Stand In Line javaScript Exercise ! Stuck!:

This worked for me!

<redacted>

Continuing the discussion from Stand In Line javaScript Exercise ! Stuck!:

Also this working and returning the intended value.

<redacted>

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

If you want to compare your solution to others, use the Get a hint button on the challenge and there are alternative solutions you can compare yours to. Also, you can probably search older posts using the forum search feature or google the challenge name and find more there.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

1 Like

This works alright but i don’t think it’s the conventional way to go about it. ie using the parameter name (item) as a variable to contain arr.shift();.

Crikey - that took me a while to understand what was happening.

1 Like

Yeap. This is the one that I’ve spent so much time with. Hours, and with lots of breaks in between.

I’ll try to explain to anyone who got to the last thread without giving you the code.

First, review your previous sections on how to push an item at the end of the array.

Second, review the section on how to create a variable that gets the first item of the array.

Third, return that variable.

Note: You don’t need to create an array. It’s already created in the form of arr.
Since you’re passing a number that is given in between parentheses, you need to specify the representation of that number, not the number. For that, you have “item”.

Let me know if you need more help with this. DM me.

Thanks for all the explanations reg shift and push.
I think I had a problem with understanding the variable “testArr”:

var testArr = [1, 2, 3, 4, 5]
...
console.log(nextInLine(testArr, item));

Therefore, afters some testings, I named the variable ‘arr’ and also created the variable ‘item‘, and so I was able to understand the usage of the props in the function ().

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

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

console.log("Before: " + JSON.stringify(arr));
console.log(nextInLine(arr, item));
console.log("After: " + JSON.stringify(arr));

I truly hope, this is not the wrong way :slight_smile:

I was stuck here for a few hours also.

Before that, I re-read the information about “queue” and I instantly knew that I could use the .push and .shift function. Easy, peasy (that’s what I thought). I typed the codes, ran it, and it failed. Then my struggle began.

Then, I went to the “requirements (instances)” and analysed every one of them.

Running the codes without changing anything else.

  // Only change code below this line
  
  return item;
  // Only change code above this line

Will pass the first two requirements:

  • nextInLine(, 5) should return a number. (Because inherently, the return item is a number)
  • nextInLine(, 1) should return 1. (Because inherently, the return item is also number 1.)

This is where you should analyse every succeeding requirement:

  • nextInLine([2], 1) should return 2

if I push item 1 to the arr, it will make the arr[2, 1]
Then remove and return 2 from the arr[2, 1] as the requirement is to return the element that was removed.
“I need to change the return into a .shift() so that it will remove the first element of the arr then return it”

  • nextInLine([5,6,7,8,9], 1) should return 5

if I push item 1, then the arr would be [5, 6, 7, 8, 9, 1]
Then return 5 from arr [5, 6, 7, 8, 9, 1] (and still removing 5 from the array)
“I definitely need to change the return into a .shift() and not item.”
To confirm the pattern, I went to the last requirement

  • After nextInLine(testArr, 10), testArr[4] should be 10.

(default) testArr = [1, 2, 3, 4, 5]

If I push item 10, it will make the testArr=[1, 2, 3, 4, 5, 10]
But the testArr[4] is “5”
Is this where I should use return .shift() to testArr?
No! Shifting testArr would return a number and 1 (and only number 1) (first two requirements). Because testArr (pushed) is [1, 2, 3, 4, 5, 10] for the function nextInLine(testArr, 10). Returning testArray (shifted, regardless if pushed or not) is always number 1.
This will not satisfy “nextInLine([2], 1) should return 2” & “nextInLine([5,6,7,8,9], 1) should return 5” because there’s no variable testArr to shift in those functions.

Tips: You only need 2 lines of codes (including return line) to pass this. Nothing fancy, just be creative.
.push() and .shift() are pretty handy.