Scopes - Exercise 4
Question 1: Shadow a Value in a Nested Function Completed
Return the local nested-function value instead of the same-named outer value.
Expected output: 2 (print this with console.log)
function scopeNestedFunctionShadow(outer, inner) {
// Write your solution here
}
console.log(scopeNestedFunctionShadow(1, 2));function scopeNestedFunctionShadow(outer, inner) {
const value = outer;
function read() { const value = inner; return value; }
return read();
}
console.log(scopeNestedFunctionShadow(1, 2));Question 2: Read Through Two Function Levels Completed
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)
function scopeReadTwoLevels(input) {
// Write your solution here
}
console.log(scopeReadTwoLevels("deep"));function scopeReadTwoLevels(input) {
const value = input;
function middle() { function inner() { return value; } return inner(); }
return middle();
}
console.log(scopeReadTwoLevels("deep"));Question 3: Prefer the Nearest Binding Completed
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)
function scopeNearestBinding(outer, middle) {
// Write your solution here
}
console.log(scopeNearestBinding("outer", "middle"));function scopeNearestBinding(outer, middle) {
const value = outer;
function parent() { const value = middle; function child() { return value; } return child(); }
return parent();
}
console.log(scopeNearestBinding("outer", "middle"));Question 4: Read Two Lexical Bindings Completed
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)
function scopeReadTwoBindings(outer, middle) {
// Write your solution here
}
console.log(scopeReadTwoBindings("a", "b"));function scopeReadTwoBindings(outer, middle) {
const first = outer;
function parent() { const second = middle; function child() { return [first, second]; } return child(); }
return parent();
}
console.log(scopeReadTwoBindings("a", "b"));Question 5: Modify the Nearest Binding Completed
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)
function scopeModifyNearestCounter(outer, middle) {
// Write your solution here
}
console.log(scopeModifyNearestCounter(5, 10));function scopeModifyNearestCounter(outer, middle) {
let count = outer;
function parent() { let count = middle; function increment() { count++; } increment(); return count; }
const changed = parent();
return [count, changed];
}
console.log(scopeModifyNearestCounter(5, 10));Question 6: Capture a Block After Exit Completed
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)
function scopeReadAfterBlockExit(input) {
// Write your solution here
}
console.log(scopeReadAfterBlockExit("kept"));function scopeReadAfterBlockExit(input) {
let read;
{ const value = input; read = () => value; }
return read();
}
console.log(scopeReadAfterBlockExit("kept"));Question 7: Compare Two Block Captures Completed
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)
function scopeTwoBlockCaptures(first, second) {
// Write your solution here
}
console.log(scopeTwoBlockCaptures("a", "b"));function scopeTwoBlockCaptures(first, second) {
let readFirst;
let readSecond;
{ const value = first; readFirst = () => value; }
{ const value = second; readSecond = () => value; }
return [readFirst(), readSecond()];
}
console.log(scopeTwoBlockCaptures("a", "b"));Question 8: Capture and Change a Block Value Completed
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)
function scopeChangedBlockCapture(first, second) {
// Write your solution here
}
console.log(scopeChangedBlockCapture("old", "new"));function scopeChangedBlockCapture(first, second) {
let read;
{ let value = first; read = () => value; value = second; }
return read();
}
console.log(scopeChangedBlockCapture("old", "new"));Question 9: Read an Outer Value in Catch Completed
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)
function scopeReadOuterInCatch(label) {
// Write your solution here
}
console.log(scopeReadOuterInCatch("handled"));function scopeReadOuterInCatch(label) {
try { throw new Error("test"); } catch (error) { return label; }
}
console.log(scopeReadOuterInCatch("handled"));Question 10: Read a Catch Binding Completed
Throw an Error with the supplied message and return its message from the catch binding.
Expected output: "failed" (print this with console.log)
function scopeCatchErrorMessage(message) {
// Write your solution here
}
console.log(scopeCatchErrorMessage("failed"));function scopeCatchErrorMessage(message) {
try { throw new Error(message); } catch (error) { return error.message; }
}
console.log(scopeCatchErrorMessage("failed"));