Step 57 “Step Inline” is impossible for me to pass

Step 57 “Step Inline” is impossible for me to pass.

It says:

Write a function nextInLine which takes an array ( arr ) and a number ( item ) as arguments.

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

So I should add function nextInline but there’s one aready on the top? I thought I can’t put two functions one below other one??? It also says to put array and number as arguments to that function. Arrays are in square brackets and numbers after coma and space (numbers like: 1,2,3,4). And in the end
“Add the number to the end of the array, then remove the first element of the array.”

I don’t know how. :frowning: I don’t know what does it even mean. Since morning for couple hours I was redoing challeneges sinc step 1 again up to 57 and it was much smoother but this still didn’t prepare me for current step.
This is what I wrote:

function nextInLine(arr, item) {
  // Only change code below this line
  function nextInLine([arr, 5]);
  return item;
  // Only change code above this line
}

// Setup
const 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));

I broke this into it’s own topic.

So I should add function nextInline but there’s one aready on the top? I thought I can’t put two functions one below other one???

You can put “two functions one below other one”. You can even define a function inside another. But you can’t have two functions with the same name in the same scope, so in that sense you are right.

What the instructions should say is "complete the function nextInline that we’ve already started for you.

It also says to put array and number as arguments to that function.

That is already done for you here:

function nextInLine(arr, item) {

Arrays are in square brackets and numbers after coma and space (numbers like: 1,2,3,4)

Yes, array literals are written in brackets with commas in between the elements if there are more than one.

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

I don’t know how. :frowning: I don’t know what does it even mean.

Do you know how to add something to the end of an array? Could you review this lesson?

There was another lesson you may want to review.

Even now I sometimes get confused about the details of some methods. I google MDN and look up the answer. Or I google “javascript remove first element of array”. Seriously - developers do a lot of googling - nobody can remember all of this stuff.

Read those lessons, start over, and see if you can get it. If you still don’t get it, check back for another hint.

And don’t get frustrated - this is tough stuff and it takes a while.

2 Likes

Yeah, I need to stop being so dramatic whenever I fail lol.

So now I get it why it is so important to take very precise notes. I’d not think I should use pop or shift in this step.

This is what I added:

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

but I get :

Before: [1,2,3,4,5]
TypeError: nextInLine.push is not a function

Also a question: like we said the challenge says

Add the number to the end of the array

In the function nextInLine the number is “item”, a word, not an actual number (like 1,2,3). I’ve put in colons “1” after “push”. I am confused.

Arguments written over there “(arr, item)” seem to me like they’re the same category. But the instruction says arr is an array and item is a number. Shoudn’t then “arr” be in square bracket? If those arguments were put in double quotes would they become strings then? I will keep googling of course and keep my head cool but I’d really appreciate the asnwer. @kevinSmith I feel already so guity to take your time haha

Edit: in here javascript - How to append something to an array? - Stack Overflow

I tried with:

 // Only change code below this line
  var arr = [1, 2, 3, 4, 5, 6];
  arr.push(7);
  arr.shift();
  return item;
  // Only change code above this line

but I get:

// running tests

nextInLine([2], 1)

should return

2
nextInLine([5,6,7,8,9], 1)

should return

5

After

nextInLine(testArr, 10)

,

testArr[4]

should be

10

// tests completed // console output Before: [1,2,3,4,5] 6 After: [1,2,3,4,5] Before: [1,2,3,4,5] 6 After: [1,2,3,4,5]

This on the right track. But what is your function returning? Have a look at that. Good luck.

EDIT: in relation to the above. Alse consider what you are doing here: arr.push(7). Is your function reusable if you put 7 in there?

1 Like

Yeah, being a dev means a lot of little failures leading to glorious success. That’s just part of the job.

As to your solution, you’re almost there. Like Slimattcode is saying, check those. Also, should you be redefining arr?

With 3 little fixes, your code passes for me.

1 Like

I have to admit I’ve put [1, 2, 3, 4, 5, 6]; radomly hoping it’d work without a clue why would it work.

But what is your function returning?
So I should erase “item” and put over there “return nextInLine”?

EDIT: in relation to the above. Alse consider what you are doing here: arr.push(7) . Is your function reusable if you put 7 in there?

I don’t know whether the function is reusable if I put 7 in there. I don’t even know why it is relevant for that function to be reusable.

I came back to this step: Write Reusable JavaScript with Functions
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/write-reusable-javascript-with-functions

Now I see “console.log” is not only for showing the output of function but I can also define function by it. In this step by putting console.log(“Hi World”); I am assigning something (value? argument? String?(because of brackets) which is “Hi World”.

I am seeing on weekend some friends who work as senior developers. Hpefully they can explain those things in my native language haha.

That is one of the primary strengths of functions. Imagine you needed a function to add two numbers together. Would you rather write out a separate function for every combination of numbers? Or would you rather pass in the two numbers as parameters so you only have to write one function.

You are pushing 7 onto the array. Reread the instructions:

Write a function nextInLine which takes an array ( arr ) and a number ( item ) as arguments.

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

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

You are adding 7 to the end of the array. What are the instructions telling you to add to the end of the array? And what is it telling you to return?

Now I see “console.log” is not only for showing the output of function but I can also define function by it.

console.log is for logging things to the console. It is a great tool for developing and debugging. I’m not sure what you mean by, “I can also define function by it”, but if you mean that you can use it inside a function, then yes. That is a very useful tool, to see what is happening.

n this step by putting console.log(“Hi World”); I am assigning something (value? argument? String?(because of brackets) which is “Hi World”.

You aren’t really assigning anything. At the risk of getting technical, console is a module that has a method called “log”. That is a fancy way of saying that console.log is a king of function. Its job is to send (log) things to the console. If I call console.log(“Hi World”);, I am passing the string literal “Hi World” to the console. But you can also pass variables. You can send more than one, separated by commas.

For example, if I were debugging your earlier code, I might put:

  var arr = [1, 2, 3, 4, 5, 6];
  console.log('arr before', arr);
  arr.push(7);
  console.log('arr after push', arr);
  arr.shift();
  console.log('arr after shift', arr);
  return item;

I am seeing on weekend some friends who work as senior developers. Hpefully they can explain those things in my native language haha.

Yeah, language makes it tough. But of course also the more you develop your English, the more job opportunities you’ll have.

1 Like

EDIT:

// Only change code below this line
  arr.push(6);
  arr.shift();
  return nextInLine;
  // Only change code above this line

after this it looks like the output is fine. Add one number at the end and remove the 1st one.

Before: [1,2,3,4,5]
[Function: nextInLine]
After: [2,3,4,5,6]

But when I run the test it shows:

nextInLine([], 5)

should return a number.

nextInLine([], 1)

should return

1
nextInLine([2], 1)

should return

2
nextInLine([5,6,7,8,9], 1)

should return

5

After

nextInLine(testArr, 10)

,

testArr[4]

should be

10

When I put return nextInLine([], 5);
it shows

Before: [1,2,3,4,5]
RangeError: Maximum call stack size exceeded

yeah, don’t return a function call, that’s a thing called recursion and you will learn about it later. If you want to test this add at the bottom of the editor console.log(nextInLine([], 5))

Also tried with this

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

this time there’s less mistakes after running test.

that’s better, but remember you need to return the removed number

Removed number is “1”

I assume it by the result:

Before: [1,2,3,4,5]
6
After: [2,3,4,5,6]

Do I comprehend it wrong?

Putting return arr.push(1); doesn’t work
The urge to click “show solution” is huge lol

Inside of a function, pretend that nothing from outside of the function exists.

function nextInLine(arr, item) {
  // Only change code below this line
  
  return item;
  // Only change code above this line
}

You only have arr and item to work with. You don’t have 1 or [1, 2, 4, 5] or any other random specific case. Only arr and item.

Look at just the first part of the requirement:

Add the number to the end of the array

How do you something to the end of an array? (You have the right method hanging around in your code)
What is it that you need to add to the end of the array?
Which array do you need to add this thing to?

function nextInLine(arr, item) {
  // Only change code below this line
  
  return item;
  // Only change code above this line
}

I see the function which is:

function nextInLine(arr, item) { return item;}

Which element is array? I guess “arr”? But shoudn’t it be in a square bracket then? The number is “item”. At this point, am I right?

How do you something to the end of an array?

I can use “.push()” on array. But I don’t see array to work with. (if it’s not “arr”).

arr is the array! The function doesn’t know where the arr was created, but it was created somewhere and passed into the function as arr when nextInLine() was called.

Which element is array? I guess “arr”?

Yes. That is where the array that you passed in is stored. That is the array you should be using.

But shoudn’t it be in a square bracket then?

Only if it is an “array literal”, if you are writing it out. If I write:

var myArray = [1, 2, 3];

then [1, 2, 3] is an array literal that is stored in the variable myArray. myArray is a variable that points to an array in memory. Inside your function, arr points to the array that is passed in.

I can use “.push()” on array.

Yes.

But I don’t see array to work with. (if it’s not “arr”).

Inside the function, it is arr.

1 Like
// Only change code below this line
  arr.push(item);
  arr.shift();
  return arr.push(item);
  
  // Only change code above this line

I am switching options, putting different lines next to “return” but nothing works. Well I’ve done 56 steps so far, each took me maximum 10-30 minutes, this one I fight since yesterday lol and it is 7 pm where I live.

What does this line do?

That doesn’t look like

The nextInLine function should then return the element that was removed

since push doesn’t remove anything.

You’re almost there. The last issue looks like your last line. You don’t want to push item, you already have. And what does push return? When in doubt, I look at the documentation. MDN is a great resource for JS. Google “mdn array push” and see what the return value is - it’s not what you want here. You may also want to google “mdn array shift” and see what that returns. [hint, hint]

1 Like

I looked up this

and did this:

// Only change code below this line
  const count = arr.push(item);
  const count1 = arr.shift();

  return count1;
  
  // Only change code above this line

and it passed. Nice… BUT I am not so certain I understand everything about this. I am afraid we’ll meet in here so much more often guys lol :confused:
I wish there were more similar tasks to this.
Thank you all for your patience

@kevinSmith or someone else, would you explain to in simple words what did I just do? Looks like when using “push/shift” etc I have to put new variable in front of it. In this case I put “count” and “count1”.

The instruction says:

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

I am still not sure what is that first element (removed one).