Hello everyone. I’m at Arrays and Function, exercise 10. I just want to confirm that what is happening in this exercise is that we are changing the value of the global scope by changing the string ‘mutated’ to ‘MUTATED’ and then .pop it off. Considering where the braces {} are I just want to confirm.
Hey everyone, i don’t understand why it is used the ‘=>’ operator before the body of the funcion (to be an operator shouldnt ‘=’ be after the ‘>’ signal?).
We are changing the elements within the array in global scope from within the function’s local/block scope. Even though the scopes are different, the function can do this because of pass-by-reference = if an array is passed into a function using an argument/parameter, any changes to the array’s elements within the function will also be made to the array outside the function. I also rationalise this by remembering that a child’s scope has access to its parent’s and ancestors’ scopes (but not vice versa) i.e. the scope hierarchy.
The first change the function makes is to reassign the value of the fourth element (index position 3) to 'MUTATED'.
The second change is to remove the last element using .pop() … pop it off as you say - love it!! … which happens to be our friend 'MUTATED' (our old friend 'mutated') again.
Why is it that in exercise 10/12 the functions used in the exercise can’t be hoisted?
I thought that functions could be hoisted.
For example, if I log to the console the array concept before I call the function changeArr, the original array is printed with none of the mutations. But function declarations should be able to be hoisted.
The topic appears to have been deleted. for some reason.
arr and newArr are both formal parameters of their respective functions. They are declared in place and are local only to the function (have function scope). They do no exist outside, nor are they accessible from the outside.
Using a function lets us add a flower to any array, not just the flowers array. This is advantageous as it allows us to write one piece of code, and then use and re-use as much as we need.
I did not understand one thing. How come (in the flowers example) the variable flowers containing the array and the function addFlower with the push method got connected. How does our program now that we want to push an element into the flowers array?
thanks for explaining. just one more question. the arr argument inside the parameter is used inside the function, but if we want change the value of the argument later in the code how would we do it? And why add the the argument inside the function why not do it like we normally do?