Scopes - Exercise 3
Question 1: Capture Let Loop Indices Completed
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)
function scopeLetLoopCaptures(count) {
// Write your solution here
}
console.log(scopeLetLoopCaptures(3));function scopeLetLoopCaptures(count) {
const readers = [];
for (let i = 0; i < count; i++) readers.push(() => i);
return readers.map((read) => read());
}
console.log(scopeLetLoopCaptures(3));Question 2: Capture a Var Loop Index Completed
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)
function scopeVarLoopCaptures(count) {
// Write your solution here
}
console.log(scopeVarLoopCaptures(3));function scopeVarLoopCaptures(count) {
const readers = [];
for (var i = 0; i < count; i++) readers.push(() => i);
return readers.map((read) => read());
}
console.log(scopeVarLoopCaptures(3));Question 3: Capture Var Indices with a Factory Completed
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)
function scopeFactoryLoopCaptures(count) {
// Write your solution here
}
console.log(scopeFactoryLoopCaptures(3));function scopeFactoryLoopCaptures(count) {
function capture(value) { return () => value; }
const readers = [];
for (var i = 0; i < count; i++) readers.push(capture(i));
return readers.map((read) => read());
}
console.log(scopeFactoryLoopCaptures(3));Question 4: Capture Per-Iteration Values Completed
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)
function scopeCaptureEachValue(values) {
// Write your solution here
}
console.log(scopeCaptureEachValue(["a","b"]));function scopeCaptureEachValue(values) {
const readers = [];
for (const value of values) readers.push(() => value);
return readers.map((read) => read());
}
console.log(scopeCaptureEachValue(["a","b"]));Question 5: Capture Incrementing Snapshots Completed
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)
function scopeCaptureRunningSnapshots(numbers) {
// Write your solution here
}
console.log(scopeCaptureRunningSnapshots([2,3,4]));function scopeCaptureRunningSnapshots(numbers) {
let sum = 0;
const readers = [];
for (const number of numbers) { sum += number; const snapshot = sum; readers.push(() => snapshot); }
return readers.map((read) => read());
}
console.log(scopeCaptureRunningSnapshots([2,3,4]));Question 6: Capture a Shared Final Sum Completed
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)
function scopeCaptureSharedFinalSum(numbers) {
// Write your solution here
}
console.log(scopeCaptureSharedFinalSum([2,3]));function scopeCaptureSharedFinalSum(numbers) {
let sum = 0;
const readers = [];
for (const number of numbers) { sum += number; readers.push(() => sum); }
return readers.map((read) => read());
}
console.log(scopeCaptureSharedFinalSum([2,3]));Question 7: Keep Each Block Label Completed
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)
function scopeCaptureBlockLabels(labels) {
// Write your solution here
}
console.log(scopeCaptureBlockLabels(["red","blue"]));function scopeCaptureBlockLabels(labels) {
const readers = [];
for (const label of labels) { const local = label; readers.push(() => local); }
return readers.map((read) => read());
}
console.log(scopeCaptureBlockLabels(["red","blue"]));Question 8: Preserve a Loop's Outer Value Completed
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)
function scopePreserveOuterLoopValue(outer, count) {
// Write your solution here
}
console.log(scopePreserveOuterLoopValue("outside", 3));function scopePreserveOuterLoopValue(outer, count) {
let value = outer;
for (let value = 0; value < count; value++) {}
return value;
}
console.log(scopePreserveOuterLoopValue("outside", 3));Question 9: Read a Var Loop Afterward Completed
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)
function scopeReadVarLoopAfterward(count) {
// Write your solution here
}
console.log(scopeReadVarLoopAfterward(4));function scopeReadVarLoopAfterward(count) {
for (var index = 0; index < count; index++) {}
return index;
}
console.log(scopeReadVarLoopAfterward(4));Question 10: Capture Index and Value Completed
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)
function scopeCaptureIndexAndValue(items) {
// Write your solution here
}
console.log(scopeCaptureIndexAndValue(["a","b"]));function scopeCaptureIndexAndValue(items) {
const readers = [];
for (let i = 0; i < items.length; i++) readers.push(() => [i, items[i]]);
return readers.map((read) => read());
}
console.log(scopeCaptureIndexAndValue(["a","b"]));