Can't understand the question - JS

Tell us what’s happening:
I am not able to understand what to do.

Your code so far


function nextInLine(arr, item) {
// Only change code below this line

return item;
// Only change code above this line


}

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

// Display code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0.

Challenge: Stand in Line

Link to the challenge:

Hey @codely!

What part of the instructions are giving you trouble?

1 Like

1. Add the number to the end of the array, then remove the first element of the array.

So you have to do two things:

  • add the number passed into the function to the end of the array passed into the function
  • remove the first element from the array passed in

2. The nextInLine function should then return the element that was removed.

In the function’s return statement you return the element that was removed from the front of the array.

2 Likes

Hey @jwilkins.oboe!

I can’t understand the whole question.

2 Likes

Thanks @bbsmooth For giving hints.

I think i have redo the whole JS basic , i can’t understand anything in JS. :frowning_face:

1 Like

Let’s address @bbsmooth’s first line.

How to add a number to the end of the array. There is helpful method that you can use that was introduced in an earlier lesson.

How to remove the first element of the array.

As i said before i have redo all the JS basic.

I am not able to understand anything in JS even after i learn C Lang.

Well it’s normal to struggle at first when you are learn a programming language.

I would suggest looking at the first link I shared. How can you apply that method to arr?

How is that going to help you add a number at the end of the array?

1 Like

First i will Learn the All that i learned before.

And thanks @jwilkins.oboe and @bbsmooth for your guide.

1 Like

Well if you want to go through the whole section again you can.

But for this problem you only need to use the two methods that I linked to above.

The solution only needs to be two lines of code.

1 Like

After doing all the JS basics.
Here is my updated code. Still, it didn’t pass

function nextInLine(arr, item) {
  // Only change code below this line
  testArr.push(6);
  testArr.shift;
  return item;
  // Only change code above this line
  

}

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

// Display code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));

1 Like

what does the challenge asks you to return?

also, to make your function reusable you must use the function parameters

1 Like

return the element that was removed.

These?

Here is a link to the hint, there is a solution there, but i think its better to try to solve the problem yourself first…

Good luck !

1 Like

are you returning the element that was removed?

also, yes those re the function parameters

1 Like

No.

and i don’t know which element to return

Thank @bedward for the hints

how are you removing the first element from the array?

1 Like

We are making good progress.

So hardcoding testArr only makes our function work for that particular array. It would be nice to re use this function for other arrays. Let’s see if we can put one of those parameters (arr, item) to good use.

Instead of hardcoding 6 let’s see if we can use those parameters that were mentioned earlier. If you reread the problem the you will know which parameter (arr or item) makes sense to add in those parenthesis.

You have a syntax error here. Please refer to the link on shift that I gave you earlier to add what is missing at the end. Also remember that I said this problem could be solved with just two lines of code. So maybe we could bump that return up to this line.

2 Likes

You are doing good and getting close. Remember a functions parameters are kind of like local variables that only exist in that function.

The array is created for you and the function is called for you at the bottom. They also already passed the values you need into your function.

I drew some arrows to try to help you understand what is getting passed where.

All you need to do is reused the values at the top inside your function code block.

4 Likes

you need a proper understanding of shift() for this, you did the push part ok.
the shift() method:

removes the first element from an array and returns that removed element. This method changes the length of the array.

Return value

The removed element from the array;

1 Like