Unit 7 Lesson 11 Assessment Question #3

I am wondering about the variable scope on CSP Unit 7 Lesson 11 Assessment Question #3. (2021-2022)

I didn’t post the code here because it is on the secure assessment.

When I look at the code in the question, I think that the global variable should be unchanged. It doesn’t seem like the function does anything to it. The global variable is simply passed to the function as an argument. But when I run it in App Lab, the global variable is updated.

I made some code that seems analogous, but uses an integer variable instead of a list and the code leaves the global variable unchanged as I expected.

function doMath(num) {
  num = num + 5;

var x = 11;

Can anyone help me understand what is going on? Thanks in advance!

The key difference is the Array. See more about arrays here Everything You Wanted to Know About App Lab Arrays, But Were Afraid to Ask Scroll down to “One more thing to know about arrays: How they work as arguments.”

Short answer: App Lab is call by value. If you change the value you change it everywhere. Objects are values that can be changed. An Array is an Object.

Numbers are values too, but they cannot be changed. You can only create a brand new one. Therefore it doesn’t get changed everywhere.

1 Like

Thank you very much! That was very helpful.