Standing in line

How does the console.log(); display one answer for line 13, and a different answer for line 15. I understand I have the function in there, but how is it showing prefunction and after function.??

   **Your code so far**
function nextInLine(arr, item) {
 // Only change code below this line
item = arr.shift()
 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));
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Stand in Line

Link to the challenge:

Do you understand what the function is doing? That is the first step to figuring this out.

1 Like

Hey there!

this is line 13?
lines are not numerated in code you posted

1 Like

Yes. So line 13 and 15 are the same. I think it’s the json.stringify doing it but not sure

JSON.stringify just prints the array out in a friendly string-like format, it is not changing the actual array.

I’ll ask again :slight_smile: Do you understand what the function is doing? This is the key as to why the two console.logs are printing out different values.

And I’ll be even more specific. Do you understand what is being passed in with the arr argument to the function? Do you understand how it is affected by the methods you are calling on it inside the function?

I think Im understanding the function. I’m. Taking off the first number with shift. Then adding the number with push. And making item =to the new array after push. Right.? So I guess what I didn’t realize was that the before: is logging it before the function and the after: was doing it after the function… I think. I was thinking it was just logging a string but it must be similar to the before and after in css.

I don’t see how this has something to do with CSS.

Lines 13, 14, and 15 are executed in that order. That’s all that is happening. There is no special meaning that changes the behavior of the code in to the text inside of the console.log calls.

Then why are the two console .logs giving different answers?

Like you said, the function is changing the testArr array. So when you log it to the console before the function then it prints out the contents of what are in it at that time. And then when you log it to the console after the function it prints out the contents that are in it at that time, which have changed since you ran it through the function.

Ok so if that’s the case then the “before:” and " after:" aren’t representing a string,. It’s telling it to log before the function. And after right.? That’s what I was wondering. Iv only seen the before and after in css so far,. Not in js. I think I understand it, if that’s what’s happening. ? And thank you

The “before:” and “after:” you have in the console.log are just strings that will be printed out to the console. They don’t have to be “before:” and “after:”. You can make them whatever you want them to be or completely remove them. They have absolutely no effect on the array. They are just there to make it easier for you to make sense of the console output.

1 Like

Ok. Well I’m still confused then. I don’t understand how it’s logging the array before my function and logging it after, when it just says console.log(testArr).

console.log is a function that will log whatever you put in between the parens. So you are telling it to log the array to the console before the function is called and then log the array to the console after the function is called. JSON.stringify converts the array to a standard string format so that it shows consistently in the console. But you can just log the array directly as well, but different browsers may show it differently in the console. So using JSON.stringify does away with those differences.

Ok. I didnt realize that the console was saving as it went. so basically, once I ran line 14 (the nextInLine function) then when I reran testArr the console had the updated version. Sorry for taking up so much time. and Thanks for responding. Much appreciated! @bbsmooth

I think you’ve basically got it now. But just to be clear, console.log is not saving anything. It merely prints out the value of whatever you put in between the parens. When you do the console.log before you call the function, testArr has the original numbers that you have assigned it, and so those number will be printed out because that’s the value of testArr at the time you call console.log. Then you call the function and the numbers in testArr change slightly. Then when you do the console.log again it prints out testArr with the new numbers in it because that’s the value of testArr at the time you call console.log.


Hey masterninjadog ,
I think it might help you if you would research the topics passed by value, passed by reference and datatypes in js.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.