Scopes - Exercise 1

Question 1: Read a Parent Function Value

Declare a message in the outer function. Return it through a nested reader function.

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

javascript

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

console.log(scopeReadParentMessage("hello"));

Question 2: Update a Parent Counter

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)

javascript

function scopeUpdateParentCounter(start) {
  // Write your solution here
}

console.log(scopeUpdateParentCounter(5));

Question 3: Keep Two Local Counters

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)

javascript

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

console.log(scopeKeepSeparateCounters(2, 8));

Question 4: Read a Shadowed Block Value

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)

javascript

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

console.log(scopeReadBlockShadow("outer", "inner"));

Question 5: Return Outer and Inner Values

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)

javascript

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

console.log(scopePairBlockValues(3, 8));

Question 6: Read a Block Assignment

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)

javascript

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

console.log(scopeAssignFromBlock(12));

Question 7: Read a Function Parameter

Create a nested function whose parameter shadows the outer argument and return the nested parameter.

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

javascript

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

console.log(scopeShadowParameter(1, 9));

Question 8: Keep an Outer Parameter

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)

javascript

function scopePreserveOuterParameter(value, inner) {
  // Write your solution here
}

console.log(scopePreserveOuterParameter(5, 9));

Question 9: Combine Two Lexical Levels

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)

javascript

function scopeCombineLexicalLevels(prefix, suffix) {
  // Write your solution here
}

console.log(scopeCombineLexicalLevels("pre", "fix"));

Question 10: Read a Var After a Block

Declare a var inside a block with the supplied value and return it after the block.

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

javascript

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

console.log(scopeReadVarAfterBlock("visible"));