Scopes - Exercise 1
Question 1: Read a Parent Function Value Completed
Declare a message in the outer function. Return it through a nested reader function.
Expected output: "hello" (print this with console.log)
function scopeReadParentMessage(message) {
// Write your solution here
}
console.log(scopeReadParentMessage("hello"));function scopeReadParentMessage(message) {
const value = message;
function read() { return value; }
return read();
}
console.log(scopeReadParentMessage("hello"));Question 2: Update a Parent Counter Completed
Create a counter in the outer function. Increment it inside a nested function and return the updated counter.
Expected output: 6 (print this with console.log)
function scopeUpdateParentCounter(start) {
// Write your solution here
}
console.log(scopeUpdateParentCounter(5));function scopeUpdateParentCounter(start) {
let count = start;
function increment() { count++; }
increment();
return count;
}
console.log(scopeUpdateParentCounter(5));Question 3: Keep Two Local Counters Completed
Create two separate local counters with the given starting values, increment the first, and return both as an array.
Expected output: [3,8] (print this with console.log)
function scopeKeepSeparateCounters(first, second) {
// Write your solution here
}
console.log(scopeKeepSeparateCounters(2, 8));function scopeKeepSeparateCounters(first, second) {
let a = first;
let b = second;
a++;
return [a, b];
}
console.log(scopeKeepSeparateCounters(2, 8));Question 4: Read a Shadowed Block Value Completed
Declare an outer value, shadow it inside a block with the supplied inner value, and return the block's value.
Expected output: "inner" (print this with console.log)
function scopeReadBlockShadow(outer, inner) {
// Write your solution here
}
console.log(scopeReadBlockShadow("outer", "inner"));function scopeReadBlockShadow(outer, inner) {
const value = outer;
{ const value = inner; return value; }
}
console.log(scopeReadBlockShadow("outer", "inner"));Question 5: Return Outer and Inner Values Completed
Declare an outer value and shadow it in a block. Return an array with the outer value first and the inner value second.
Expected output: [3,8] (print this with console.log)
function scopePairBlockValues(outer, inner) {
// Write your solution here
}
console.log(scopePairBlockValues(3, 8));function scopePairBlockValues(outer, inner) {
const value = outer;
let inside;
{ const value = inner; inside = value; }
return [value, inside];
}
console.log(scopePairBlockValues(3, 8));Question 6: Read a Block Assignment Completed
Declare an outer let variable. Assign the supplied value inside a block and return it after the block.
Expected output: 12 (print this with console.log)
function scopeAssignFromBlock(input) {
// Write your solution here
}
console.log(scopeAssignFromBlock(12));function scopeAssignFromBlock(input) {
let result;
{ result = input; }
return result;
}
console.log(scopeAssignFromBlock(12));Question 7: Read a Function Parameter Completed
Create a nested function whose parameter shadows the outer argument and return the nested parameter.
Expected output: 9 (print this with console.log)
function scopeShadowParameter(outer, inner) {
// Write your solution here
}
console.log(scopeShadowParameter(1, 9));function scopeShadowParameter(outer, inner) {
function read(outer) { return outer; }
return read(inner);
}
console.log(scopeShadowParameter(1, 9));Question 8: Keep an Outer Parameter Completed
Call a nested function whose parameter has the same name as the outer parameter, then return the unchanged outer parameter.
Expected output: 5 (print this with console.log)
function scopePreserveOuterParameter(value, inner) {
// Write your solution here
}
console.log(scopePreserveOuterParameter(5, 9));function scopePreserveOuterParameter(value, inner) {
function use(value) { return value; }
use(inner);
return value;
}
console.log(scopePreserveOuterParameter(5, 9));Question 9: Combine Two Lexical Levels Completed
Declare a prefix in the outer function and use it with a suffix inside a nested function. Return their concatenation.
Expected output: "prefix" (print this with console.log)
function scopeCombineLexicalLevels(prefix, suffix) {
// Write your solution here
}
console.log(scopeCombineLexicalLevels("pre", "fix"));function scopeCombineLexicalLevels(prefix, suffix) {
const start = prefix;
function combine(end) { return start + end; }
return combine(suffix);
}
console.log(scopeCombineLexicalLevels("pre", "fix"));Question 10: Read a Var After a Block Completed
Declare a var inside a block with the supplied value and return it after the block.
Expected output: "visible" (print this with console.log)
function scopeReadVarAfterBlock(input) {
// Write your solution here
}
console.log(scopeReadVarAfterBlock("visible"));function scopeReadVarAfterBlock(input) {
if (true) { var value = input; }
return value;
}
console.log(scopeReadVarAfterBlock("visible"));