testArr and arr are the same?

Thanks in Advance.

My answer works, but I am not sure what I am doing.

  1. testArr = [1, 2, 3, 4, 5]; OK
    But where do I declare that (arr = [1, 2, 3, 4, 5]; ) ?

  2. Where do I declare that (ítem = 6) if this is what I am doing ?

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

const testArr = [1, 2, 3, 4, 5];
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));


function nextInLine(arr, item) 
{arr.push(item);
let removed = arr.shift();
return removed;}
const testArr = [1, 2, 3, 4, 5];
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 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15

Challenge: Stand in Line

Link to the challenge:

arr is one of the function parameters

its value is determined when the function is called

2 Likes

I see. Thanks a lot.

What about “item”?
There is already when I declare: function nextInLine(arr, item)
Why am I asked to append it again with push ?

function nextInLine(arr, item)
{arr.push(item);
let removed = arr.shift();
return removed;}
const testArr = [1, 2, 3, 4, 5];
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));

it’s not in arr tho, you need to add it to arr, that’s the first part of the challenge

Sorry, I don´t get it.

If I add (ítem) it to arr, shouldn´t the console show
[2,3,4,5,item, 6]
after I removed 1 ?

item is not the string literal “item”. Try logging what item is when you call the function.

1 Like

Sorry as a beginner I am quite Lost. I made comments below on what I guess I´m doing:

function nextInLine(arr, item)
{arr.push(item) // I apend (item) once more to nextInLine, correct?
let removed = arr.shift(); // What is the first element of arr that I am removing?
return removed;} // Why is this 1, if there are no numbers declared as arr ?
const testArr = [1, 2, 3, 4, 5];
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));

When in doubt, log it out!

function nextInLine(arr, item)  {
  console.log("** arr:", arr);
  console.log("** item:", item);
  arr.push(item);
  console.log("** arr after pushing:", arr);
  let removed = arr.shift();
  console.log("** element removed:", removed);
  return removed;
}

Thanks Ilenia and Jeremy, but I don´t know how to

Do I supone to asign a value to (arr) and to (ítem) ?

It will make sense to me if the Challenge obliged me to declare
let arr = [1, 2, 3, 4, 5];
let ítem = [1];

Sorry, being a beginner demands silly explanations.
Thanks for your patience.

Did you run the code I gave you? I showed you how to log out the values the function is using! Playing around with code and running different stuff is key in trying to understand how it works.


If you did this, then your function would no longer be usable as a reusable function. It would only work for that one array and one item and could never use any other arrays.

This right here says that when the function is called, the specific arr and item will be passed into the function.

And here where the function is called, we see that arr === testArr and item === 6.

Sorry.
I don´t quite understand the code you gave me.
I don´t think I have studied that “** :” code yet, neither I know what “log out” means in this case.

Thanks, that I can understand, because I used let to declare those valúes and are not global.

I fail to see

when the function is called.

I see that
const testArr = [1, 2, 3, 4, 5]; has the value of “1” in the beginning. Ok
But I don´t understand where the number “1” comes from when I remove the first element of “arr” , since I don´t find where it is declared that that number is part of “arr”.

Thanks for your help.

You don’t need to “study” it. You need to copy-paste it into your browser and “run” it. Studying it won’t help you literally see what happens when the code runs.

Please, stop for a second, copy the code I gave you in place of your solution, and show me what output it gives you.

arr.shift();
removed the first element of arr. Ok
but I don´t find where it says that this element is “1”,
since I would expect to had it declared somewhere like:
arr = [1, 2, 3, 4, 5];

Sure. Sorry.
It shows me an error:

function nextInLine(arr, item)
{arr.push(item)
let removed = arr.shift();
return removed;}
const testArr = [1, 2, 3, 4, 5];
console.log(“Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log(“After: " + JSON.stringify(testArr));
unction nextInLine(arr, item) {
console.log(”** arr:”, arr);
console.log("** item:", item);
arr.push(item);
console.log("** arr after pushing:", arr);
let removed = arr.shift();
console.log("** element removed:", removed);
return removed;
}

Put it in place of your solution. You can’t paste in a second definition of a function.

This error:

SyntaxError: unknown: Missing semicolon. (9:7)

7 | console.log(nextInLine(testArr, 6));
8 | console.log("After: " + JSON.stringify(testArr));

9 | unction nextInLine(arr, item) {
| ^
10 |
11 | console.log("** arr:", arr);
12 | console.log("** item:", item);

You have copy-paste errors

function nextInLine(arr, item) {
  // Only change code below this line
  console.log("** arr:", arr);
  console.log("** item:", item);
  arr.push(item);
  console.log("** arr after pushing:", arr);
  let removed = arr.shift();
  console.log("** element removed:", removed);
  return removed;
  // 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));

Yes, the solution I put in the first place it worked.
I only did not understand how it would Pick “1” as the value to be removed from “arr” since I don´t see where it was declared that “1” was part of arr.

In
const testArr = [1, 2, 3, 4, 5];
testArr had its values declared and I can see that the first element is “1”.
But arr has not such declaración.

Thanks.

Dude. Did you actually run the code I provided?

Those console.log statements give us something to work with.

Yes, I copied after my code and run it.
Will do it again.