Scopes - Exercise 3

Question 1: Capture Let Loop Indices

Create one function per iteration of a let loop from 0 to count - 1. Call the functions afterward and return their results.

Expected output: [0,1,2] (print this with console.log)

javascript

function scopeLetLoopCaptures(count) {
  // Write your solution here
}

console.log(scopeLetLoopCaptures(3));

Question 2: Capture a Var Loop Index

Create one function per iteration of a var loop from 0 to count - 1. Call them after the loop and return their results.

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

javascript

function scopeVarLoopCaptures(count) {
  // Write your solution here
}

console.log(scopeVarLoopCaptures(3));

Question 3: Capture Var Indices with a Factory

Use a var loop and a function factory to capture each index separately. Call the functions after the loop and return their results.

Expected output: [0,1,2] (print this with console.log)

javascript

function scopeFactoryLoopCaptures(count) {
  // Write your solution here
}

console.log(scopeFactoryLoopCaptures(3));

Question 4: Capture Per-Iteration Values

Create a closure for each array value using a for...of loop with const. Call the closures afterward and return their values.

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

javascript

function scopeCaptureEachValue(values) {
  // Write your solution here
}

console.log(scopeCaptureEachValue(["a","b"]));

Question 5: Capture Incrementing Snapshots

For each number, capture the running sum in a block-scoped constant and create a reader for that snapshot. Return the readers' results.

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

javascript

function scopeCaptureRunningSnapshots(numbers) {
  // Write your solution here
}

console.log(scopeCaptureRunningSnapshots([2,3,4]));

Question 6: Capture a Shared Final Sum

Create a reader of the same running sum during each iteration, then return all reader results after processing the numbers.

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

javascript

function scopeCaptureSharedFinalSum(numbers) {
  // Write your solution here
}

console.log(scopeCaptureSharedFinalSum([2,3]));

Question 7: Keep Each Block Label

For each input label, declare a block-scoped local and store a function that returns it. Return the function results.

Expected output: ["red","blue"] (print this with console.log)

javascript

function scopeCaptureBlockLabels(labels) {
  // Write your solution here
}

console.log(scopeCaptureBlockLabels(["red","blue"]));

Question 8: Preserve a Loop's Outer Value

Declare an outer value, use a let loop variable of the same name for count iterations, and return the unchanged outer value.

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

javascript

function scopePreserveOuterLoopValue(outer, count) {
  // Write your solution here
}

console.log(scopePreserveOuterLoopValue("outside", 3));

Question 9: Read a Var Loop Afterward

Use a var loop from zero while the counter is less than count, then return the counter after the loop. Count is non-negative.

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

javascript

function scopeReadVarLoopAfterward(count) {
  // Write your solution here
}

console.log(scopeReadVarLoopAfterward(4));

Question 10: Capture Index and Value

Using a let loop, create readers that return an array containing the index and its corresponding item. Call them afterward.

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

javascript

function scopeCaptureIndexAndValue(items) {
  // Write your solution here
}

console.log(scopeCaptureIndexAndValue(["a","b"]));