After my first reading, I said myself that the fact to put the global variable as argument protect the global variable of mutating.
So I tried this two litle code in oder to verify if I well understand:
// The global variable
var fixedValue = 4;
function incrementer () {
// Only change code below this line
return ++fixedValue
// Only change code above this line
}
console.log(incrementer()) // 5
console.log(fixedValue) // 5
Thus fixedValue is not protected.
// The global variable
var fixedValue = 4;
function incrementer (x) {
// Only change code below this line
return ++x
// Only change code above this line
}
console.log(incrementer(fixedValue)) // 5
console.log(fixedValue) // 4
Thus super, fixedValue is protected.
Did I well understand the meaning of these exercises?
Parameters are just variables local to the function.
You are assigning the value of fixedValue to the x parameter variable and changing the value of it instead of changing fixedValue. The x parameter just contains a copy of the value.
This:
let globalVariable = 10;
function localScope(localVariable) {
localVariable += 1;
return localVariable;
}
console.log(localScope(globalVariable)); // 11
console.log(globalVariable); // 10
Is functionally the same as this:
let globalVariable = 10;
function localScope() {
let localVariable = globalVariable;
localVariable += 1;
return localVariable;
}
console.log(localScope()); // 11
console.log(globalVariable); // 10
It doesn’t really protect the global it just doesn’t directly modify it. However, if the value being copied is of an object type you will still mutate it.
// the global variable
var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];
/* This function should add a book to the list and return the list */
// New parameters should come before bookName
// Add your code below this line
function add(arr, bookName) {
let newArr = [...arr]; // Copy the bookList array to a new array.
newArr.push(bookName); // Add bookName parameter to the end of the new array.
return newArr; // Return the new array.
}
/* This function should remove a book from the list and return the list */
// New parameters should come before the bookName one
// Add your code below this line
function remove(arr, bookName) {
let newArr = [...arr]; // Copy the bookList array to a new array.
if (newArr.indexOf(bookName) >= 0) {
// Check whether the bookName parameter is in new array.
newArr.splice(newArr.indexOf(bookName), 1); // Remove the given paramater from the new array.
return newArr; // Return the new array.
}
}
var newBookList = add(bookList, 'A Brief History of Time');
var newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies');
var newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');
console.log(bookList);
Because you are working with/on a different variable in a different scope.
I know the challenge uses the word “precedence” and I guess that is one way of thinking about it, but I also think it might potentially give the wrong impression. Precedence sounds like a priority system, but really it’s just first come first serve based on scopes. Edit: I guess first come first serve is a priority system, but it’s very simple and easy to understand. The word precedence in my mind makes it sound like something fancier might be going on.
The identifier (variable name) is resolved by starting from the closest scope to the usage and moving outwards/upwards. If it finds the identifier in an enclosing scope it stops looking and uses that identifier. There might be a hundred other variables named the same (hopefully not) living in scopes further out/up.
Imagine being on the ground floor of a building and looking for a person named John. You start at the first floor and start looking at the nameplates on each door then moving up one floor, and so on. If you find a flat with the name John you stop looking. There might be 10 more people named John living higher up in the building but you are just looking for the first one you find.
Yes, because in that case, you are not getting a copy of a primitive value but a reference.
We don’t need to use “Object.assign()” to make copy of table because the copy is done inside a function ?
// the global variable
var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];
/* This function should add a book to the list and return the list */
// New parameters should come before bookName
// Add your code below this line
function add(arr, bookName) {
let newArr = [...arr]; // Copy the bookList array to a new array.
newArr.push(bookName); // Add bookName parameter to the end of the new array.
return newArr; // Return the new array.
}
/* This function should remove a book from the list and return the list */
// New parameters should come before the bookName one
// Add your code below this line
function remove(arr, bookName) {
let newArr = [...arr]; // Copy the bookList array to a new array.
if (newArr.indexOf(bookName) >= 0) {
// Check whether the bookName parameter is in new array.
newArr.splice(newArr.indexOf(bookName), 1); // Remove the given paramater from the new array.
return newArr; // Return the new array.
}
}
var newBookList = add(bookList, 'A Brief History of Time');
var newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies');
var newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');
console.log(bookList);
Are you referring to the array copy done using spread inside the functions? If so it doesn’t matter where you make the copy, at least not in regards to it being a copy, but it makes sense to do it at that location and at that time in the code execution.
No, passing an argument to a parameter is the same as a normal variable assignment. It does not create a copy.
Not sure what specifically in the code you posted you are referring to but it is using spread syntax to make copies of the array. The challenge you linked to is using an array, not an object Object.assign() is for objects.