Parameters and Arguments

I’d like to draw someone’s attention to this question that came up as I was working on the challenge here. Concerning, the logic of the implementation: the function takes two parameters- an array and an item ( a number) CHECK. The next line, arr.push(item) is actually pushing to an array AND it is taking in the numbers that are being pushed (THIS IS WHY WE NEED TO DEFINE THE PARAMETER: CAUSE THE ARRAY HAS NUMBERS IN IT - THUS MAKING THE PARAMETERS (ARR, ITEM)

THIS actual array is pushed into an empty array, RIGHT? This means that I’d be able to potentially assign this .push(item) to a variable and use that variable for whatever computation should need be, RIGHT? Am I grasping this?

Could anyone please refer me to any other tutorials that touch on this specific topic, please?


function nextInLine(arr, item) {
  // Only change code below this line
  let localM = arr.push(item); 
  
  return arr.shift();
  // 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));
//   console.log(arr.length); 
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36

Challenge: Stand in Line

Link to the challenge:

No. You are always working with the array passed into the function (through the function parameter arr). This array represents the queue. You want to add the item to the end of the queue and then remove the first item from the beginning of the queue and return it. All of this will be done to arr.

2 Likes

Here are some tutorials on functions and arrays.
https://www.javascripttutorial.net/javascript-array/
https://www.javascripttutorial.net/javascript-function/

1 Like

Awesome! Thanks again.

Thanks again! Just took a peek…

do you refer to the this keyword? In the current context, the function has its this undefined. Its a bit more complicated to bind a this to the function, so you can refer and work with it.
In the current context, the nextInLine function is very basic. Like you can see, it just takes two arguments, it assumes the first argument(arr) is an array. In the function body, wherever you use “arr”, you refer to this exact parameter, whatever you passed to the function to represent it. push() and shift() ar array methods, which directly mutate the array they are called on, i.e. they change the array they are called on. You use them on the array stored as “arr”, within the function. The item parameter, is of unknown type, if we are to neglect the challenge description. It could be anything, a number, an array, an object, a string…You push the item to the subject array(on its end), you remove its first item, then you want to return the current state of the array, in order to fulfill the challenge.

Hey Silvant, thanks for the input.

It’s also worth knowing that arrays (and other objects) are passed by reference. So when changing the object you are manipulating the passed in object. I think might be where some if your confusion is coming from.

Primitive values such as boolean, string and number (technically these are objects too) are passed by value. So you are manipulating a local copy of the value and the value in the calling function is unaffected.

1 Like

There is some semantics around this that can make it confusing.

You can change the properties (members) but you can not change the object. Which is worth knowing as well.

const user1 = {
  name: 'John',
};

const user2 = {
  name: 'Jack',
};

function changeUser(firstUser, secondUser) {
  firstUser.name = 'Mary';

  console.log(secondUser); // { name: 'Jack' }
  
  secondUser = { name: 'Neo' };
  secondUser.name = 'Morpheus';
  
  console.log(secondUser); // { name: 'Morpheus' }
}

changeUser(user1, user2);

console.log(user1); // { name: 'Mary' }
console.log(user2); // { name: 'Jack' }

Just to clarify, JS doesn’t support passing by reference, it only supports passing by value or by sharing (copying). The key thing in this case is that the contents of an object (in this case an array) can be modified, so if you have access to the value of the reference (in this case a copy of the reference value) you can modify the contents.

A language with pass-by-reference would allow you to do this and have myObj change:

myObj = { someProperty: 1 };

function changeObj (obj) {
  obj = {};
}

changeObj(myObj);
console.log(myObj);

But that doesn’t do anything except set the variable obj inside the scope of the function to an empty object, nothing else, because the reference is not being passed.

Thanks for the clarification. JavaScript always passes by value but sometimes that value is a reference. When manipulating the contents/properties of an object the difference should be imperceptible but it’s good to know what’s going on, especially if you try reassigning that reference.