0

I have been working on this for 3 hours exploring videos on scope, functions, function strings, and variable parameters. But I cannot get it right!

Quiz on Scope

Test your understanding of variable visibility.

var outside = 'outside';

function a() {
  var aVar = 'a';

  function aInner1() {
    var aInner1Var = 'aInner1Var';

    function aInner1Inner() {
      var aInner1InnerVar = 'aInner1InnerVar'; 
    }
  }

  function aInner2(aInner2Param) {
    var aInner2Var = aInner2Param;
  }
}

function b(bParam) {
  console.log(bParam);
}

Which of the following statements are true for the above code? Select 1 or more.

  1. a is visible to b
  2. b and bParam are visible to a
  3. aInner1InnerVar is only visible to aInner1Inner and aInner1
  4. aInner1InnerVar is visible to aInner1Inner
  5. aVar is visible to aInner2
  6. aInner1Var is visible to aInner2
  7. aInner2Param is visible to aInner1

My thoughts so far:

I know a local function can see the parent function, (basically anything above it). But I don't know if it can see only the function name. Or also the parameter/variable/etc. And I know all functions can see global variables.

my attempt to pseudocode

function one() {            // one can see oneA, oneB, oneAB, two
   function oneA() {        // oneA can see one, oneB  
       function oneB() {    // oneB can see one, oneA, oneB 
     }
   }
   function oneAB() {       // oneAB can see one, two
     }
}
function two() {            // two can see one
}
  1. a is visible to b // global scope function

    • YES
  2. b and bParam are visible to a // function name "a/b" yes, not sure for param

    • ?
  3. aInner1InnerVar is only visible to aInner1Inner and aInner1 // var in block visible to function and outer function

    • ?
  4. aInner1InnerVar is visible to aInner1Inner // var in block visible to function

    • ?
  5. aVar is visible to aInner2 // function can see var inside another function

    • NO
  6. aInner1Var is visible to aInner2 // function can see var of funct within a funct

    • NO
  7. aInner2Param is visible to aInner1

    • NO
Timothy
  • 1
  • 2
  • "*I cannot get it right!*" - what are you getting? What is your proposed answer, and where in particular do you have problems choosing? Which parts do you understand, which not? – Bergi Nov 16 '22 at 20:03
  • Just updated the question @Bergi thank you for the notes! – Timothy Nov 16 '22 at 20:08
  • @Bergi I understand the idea of: Local /global/block scope. I know var has global access. But as far as assessing visibility of a function (question 1), visibility of parameters and variables within different levels of 3 functions within 1. I am very confused and having trouble finding a resource to consult that directly advises on this. I have checked functions/scope on MDN, W3Schools, and youtube, now here also. But I cannot move forward. – Timothy Nov 16 '22 at 20:09
  • Please do not post a link to a painting of code or text! Quote the text itself, and format the code. You can edit your question to update it – Luis Fernando Nov 16 '22 at 20:09
  • @LuisFernando updated! Thank you for the notes, new to the community. – Timothy Nov 16 '22 at 20:20
  • @Bergi I added a ton more of my thought process to the question. Let me know if you have any pointers it would be much appreciated! Thank you – Timothy Nov 16 '22 at 21:00
  • It's not quite clear what it means for "a function" to "see" a variable (or, for a variable to be visible "to" a function). Does it refer to code in the function (at the top level of the function body, in particular) seeing the variable? – Bergi Nov 16 '22 at 21:13
  • @Bergi the question is on variable visibility (scope) so for 1-7 it is asking if the "blank" is within the scope of "Blank". Assuming it means if it is visible/in scope/aka accessible. EX: #5. aVar is visible to aInner2 - is aVar within the scope of access/visibility of the function aInner2. – Timothy Nov 16 '22 at 21:18
  • Btw, did you trying running the code? Add some `console.log` statements, or even better `debugger;` breakpoints, and call the functions. – Bergi Nov 16 '22 at 21:19
  • @Bergi I have been trying that also. This is supposed to be a very simple entry level exercise but its been 5 hrs and I cannot move forward in the lesson – Timothy Nov 16 '22 at 21:22

1 Answers1

0
  • 1 = yes - function a and b are in the same scope

  • 4 = yes - the variable is in the body of the function / in scope

  • 5 = yes - the variable is within the parent function

  • 2, 3, 6, 7 = no

Timothy
  • 1
  • 2