Scopes - Exercise 4

Question 1: Shadow a Value in a Nested Function

Return the local nested-function value instead of the same-named outer value.

Expected output: 2 (print this with console.log)

javascript

function scopeNestedFunctionShadow(outer, inner) {
  // Write your solution here
}

console.log(scopeNestedFunctionShadow(1, 2));

Question 2: Read Through Two Function Levels

Declare the supplied value in the outer function, then return it from a function nested inside another function.

Expected output: "deep" (print this with console.log)

javascript

function scopeReadTwoLevels(input) {
  // Write your solution here
}

console.log(scopeReadTwoLevels("deep"));

Question 3: Prefer the Nearest Binding

Declare a value in the outer function and a same-named value in a middle function. Return the value read by an inner function.

Expected output: "middle" (print this with console.log)

javascript

function scopeNearestBinding(outer, middle) {
  // Write your solution here
}

console.log(scopeNearestBinding("outer", "middle"));

Question 4: Read Two Lexical Bindings

Declare one value in the outer function and one in a middle function. Return both from a nested function in an array.

Expected output: ["a","b"] (print this with console.log)

javascript

function scopeReadTwoBindings(outer, middle) {
  // Write your solution here
}

console.log(scopeReadTwoBindings("a", "b"));

Question 5: Modify the Nearest Binding

Create an outer and a shadowing middle counter. Increment the middle counter from an inner function and return both counters.

Expected output: [5,11] (print this with console.log)

javascript

function scopeModifyNearestCounter(outer, middle) {
  // Write your solution here
}

console.log(scopeModifyNearestCounter(5, 10));

Question 6: Capture a Block After Exit

Create a block-scoped value and a function that reads it. Call the function after the block and return its result.

Expected output: "kept" (print this with console.log)

javascript

function scopeReadAfterBlockExit(input) {
  // Write your solution here
}

console.log(scopeReadAfterBlockExit("kept"));

Question 7: Compare Two Block Captures

Create one reader in each of two blocks, capturing the respective input. Return the readers' results.

Expected output: ["a","b"] (print this with console.log)

javascript

function scopeTwoBlockCaptures(first, second) {
  // Write your solution here
}

console.log(scopeTwoBlockCaptures("a", "b"));

Question 8: Capture and Change a Block Value

Create a reader for a block-scoped let value, change that value before leaving the block, and return the reader's result afterward.

Expected output: "new" (print this with console.log)

javascript

function scopeChangedBlockCapture(first, second) {
  // Write your solution here
}

console.log(scopeChangedBlockCapture("old", "new"));

Question 9: Read an Outer Value in Catch

Throw an error inside a try block, then return the supplied outer label from the catch block.

Expected output: "handled" (print this with console.log)

javascript

function scopeReadOuterInCatch(label) {
  // Write your solution here
}

console.log(scopeReadOuterInCatch("handled"));

Question 10: Read a Catch Binding

Throw an Error with the supplied message and return its message from the catch binding.

Expected output: "failed" (print this with console.log)

javascript

function scopeCatchErrorMessage(message) {
  // Write your solution here
}

console.log(scopeCatchErrorMessage("failed"));