Basic JavaScript: Stand in Line I need an explanation what this is about and how to pass it

Tell us what’s happening:
The first objective is to " Write a function nextInLine which takes an array ( arr ) and a number ( item ) as arguments.", and I won’t know what it means. Can someone explain it please.

There are three more checkpoints I need to pass.
Your code so far

function nextInLine(arr, item) {

// Only change code below this line

return item;

// Only change code above this line

function nextInLine(arr[1,2,3,4,5], items) {

}

}

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


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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36.

Challenge: Stand in Line

Link to the challenge:

1 Like

Hello!

I think you cannot understand what the item is without the entire context:

As you can see, item is just the number to be added as the end of the array.

Now, why it’s called item instead of number it’s because a we’re learning about queues, which are, usually, used to store general type of items, not just numbers.

Of course, I’m assuming you know what an array, a parameter and a function are.

I hope it helps :slight_smile:,

Regards!

I think it helps to write down what you need to do in bullet points.

These two sentences say what you need to do…

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.

  1. add item to end of arr
  2. remove 1st element of arr
  3. return element that was removed

So you must push an item, shift an element, and return the element that was removed (the first indexed element initially.) I would store that first indexed element in a variable before I remove it. Lmk if you need more help! :blush:

1 Like

Thank you for your reply, I know what are the following things you have listed are. I’m just confused at this whole challenge, below this is the code I have put inside the comments that indicate there to put the code.

function nextInLine(arr[1,2,3,4,5], item);
item.push();
item.shift();
return item;

And I didn’t pass one checkmark, so can you tell me what am I suppose to do first, like what does it mean write a function nextInLine which takes an array ( arr ) and a number ( item ) as arguments.?

Thank you for your repily. First I need to create an array, is this how I do it? function nextInLine(arr[1,2,3,4,5], item)?

Dan, the arrays are provided for you so you don’t need to create an array. Your function has to work no matter what array is inserted so the top of your function should be left untouched…

function nextInLine(arr, item) {

In comparison with math, to me - functions are like the formulas and you insert variables into it.

I would try to understand fully what a function is and what arrays and the array methods can do, since I believe if you try to push on from here you will not fully grasp the rest of what is to come. Wish you the best!

Ok, I have changed my function, and this is my code so far…
function nextInLine(arr, item) {

}

return item;

What do I add inside the function?

That means that you have to write a function that takes two arguments.

The challenge specifies that the arguments are called arr and item respectively and that the first is an array (which may or may not be empty, you don’t know) and the second one is a number.

To give you an example, let’s assume someone tells me:

Write a function called theJoiner that takes two arguments, the first one will be a string called str and the second one will be a number called n . Make a string out of both and return it.

This means I have to write a function like this:

function theJoiner(str, n) {
  return str + n;
}

In the challenge, the editor is pre-filled with the function, you just have to write the logic of the function, as @CaraLagumen said.

Finally, in regards to this code:

function nextInLine(arr[1,2,3,4,5], item);
item.push();
item.shift();
return item;

Assuming this is meant to be pseudo-code (not actual JavaScript, otherwise it’s completely wrong):

  • you already have the array, there’s no need to define it (arr[1...5] is not right).
  • Take into account the type of the parameters as specified in the challenge and read the second and third lines, can you see the error? (Analise it :slight_smile:).

This should answer your last comment too :stuck_out_tongue:.

1 Like

shift and push are array methods,
you can’t use them on numbers.
just in case this was a genuine try

try to do one requisite at a time, in order.

So, first thing add the item at the end of the array.
Can you do this?

the nextInLine function that you write will be called and testArr and item passed in as arguments like this:

nextInLine([1, 2, 3, 4, 5 ], 6);

The function body of nextInLine now has access to the testArr and item so it can work with them inside the function body.

  • Inside the function body we need to add the item to the end of testArr
  • remove and return the item in the first index of testArr

All arrays created in Javascript have access to a collection of methods (built in functions). Check out: Array methods on the MDN website and get familiar with all the methods available on arrays!

To call a method on an array we reference the array by name and then use dot notation to access and invoke that method on the array, like this:

array.method();

Some methods expect 1 or more arguments to be passed into their parentheses.
The push method is one method that expects an argument to be passed in (the item you want to add to the end of the array).

Some methods do not expect arguments to be passed into their parenthesis.
The shift method is one that does not expect an argument (because it removes the item at the first index of an array and also returns the item that was removed).

If a method or function returns a value, the value returned can be captured in a variable like so:

let value = someFunction();

Essentially we are saying: “let value contain the returned value of calling someFunction.”

I hope this helps you figure it out :slight_smile:

I decided to restart the challenge, first how do make nextInLine() function return a number?

So I found the solution of the challenge, albeit I’m still not sure about the logic behind it, I’m not sure what the code’s purpose for, and how the different parts like .push() and .shift() function, like how to write and what they do?

check documentation on them
push() will add its arguments at the end of the array it is used on
shift() remove the first element in the array and return it

Thanks I already solved, whose were the issues.