Scopes - Exercise 2
Question 1: Capture an Addition Amount Completed
Create a function that closes over the supplied increment and adds it to the supplied value. Call it and return the result.
Expected output: 10 (print this with console.log)
function scopeCaptureAddition(increment, value) {
// Write your solution here
}
console.log(scopeCaptureAddition(3, 7));function scopeCaptureAddition(increment, value) {
function add(number) { return number + increment; }
return add(value);
}
console.log(scopeCaptureAddition(3, 7));Question 2: Call a Counter Three Times Completed
Create a closure holding start. Each call increments it by step. Return the results of three calls.
Expected output: [5,8,11] (print this with console.log)
function scopeThreeCounterCalls(start, step) {
// Write your solution here
}
console.log(scopeThreeCounterCalls(2, 3));function scopeThreeCounterCalls(start, step) {
let count = start;
function next() { count += step; return count; }
return [next(), next(), next()];
}
console.log(scopeThreeCounterCalls(2, 3));Question 3: Create Independent Counters Completed
Create two counter closures starting at the supplied values. Each call increments its own counter by one. Return the result of calling the first, second, then first again.
Expected output: [2,11,3] (print this with console.log)
function scopeIndependentCounters(first, second) {
// Write your solution here
}
console.log(scopeIndependentCounters(1, 10));function scopeIndependentCounters(first, second) {
function makeCounter(start) { let count = start; return () => ++count; }
const a = makeCounter(first);
const b = makeCounter(second);
return [a(), b(), a()];
}
console.log(scopeIndependentCounters(1, 10));Question 4: Capture a Prefix in a Closure Completed
Create a function that prefixes each supplied word with prefix. Return the results for both words.
Expected output: ["pre-one","pre-two"] (print this with console.log)
function scopePrefixTwoWords(prefix, first, second) {
// Write your solution here
}
console.log(scopePrefixTwoWords("pre-", "one", "two"));function scopePrefixTwoWords(prefix, first, second) {
function addPrefix(word) { return prefix + word; }
return [addPrefix(first), addPrefix(second)];
}
console.log(scopePrefixTwoWords("pre-", "one", "two"));Question 5: Read a Changed Captured Value Completed
Create a function that reads an outer variable, change that variable to the new value, then call the reader and return its result.
Expected output: "new" (print this with console.log)
function scopeReadChangedCapture(initial, replacement) {
// Write your solution here
}
console.log(scopeReadChangedCapture("old", "new"));function scopeReadChangedCapture(initial, replacement) {
let value = initial;
function read() { return value; }
value = replacement;
return read();
}
console.log(scopeReadChangedCapture("old", "new"));Question 6: Accumulate Closure Deposits Completed
Create a closure holding the starting balance. Call it with each deposit and return the final balance.
Expected output: 15 (print this with console.log)
function scopeAccumulateDeposits(start, deposits) {
// Write your solution here
}
console.log(scopeAccumulateDeposits(10, [2,3]));function scopeAccumulateDeposits(start, deposits) {
let balance = start;
function deposit(amount) { balance += amount; }
for (const amount of deposits) deposit(amount);
return balance;
}
console.log(scopeAccumulateDeposits(10, [2,3]));Question 7: Capture a Private Name Completed
Create a closure that returns a greeting using the supplied name. Call it with the supplied greeting and return the result separated by a space.
Expected output: "Hello Ava" (print this with console.log)
function scopePrivateNameGreeting(name, greeting) {
// Write your solution here
}
console.log(scopePrivateNameGreeting("Ava", "Hello"));function scopePrivateNameGreeting(name, greeting) {
function greet(prefix) { return prefix + " " + name; }
return greet(greeting);
}
console.log(scopePrivateNameGreeting("Ava", "Hello"));Question 8: Update a Captured Setting Completed
Create a reader and writer that share a local setting. Write the new value and return what the reader sees.
Expected output: "on" (print this with console.log)
function scopeSharedSetting(initial, next) {
// Write your solution here
}
console.log(scopeSharedSetting("off", "on"));function scopeSharedSetting(initial, next) {
let setting = initial;
function read() { return setting; }
function write(value) { setting = value; }
write(next);
return read();
}
console.log(scopeSharedSetting("off", "on"));Question 9: Capture Separate Multipliers Completed
Create two multiplier closures using the supplied factors. Return their results when both are called with the same value.
Expected output: [8,12] (print this with console.log)
function scopeSeparateMultipliers(first, second, value) {
// Write your solution here
}
console.log(scopeSeparateMultipliers(2, 3, 4));function scopeSeparateMultipliers(first, second, value) {
function makeMultiplier(factor) { return (number) => factor * number; }
const a = makeMultiplier(first);
const b = makeMultiplier(second);
return [a(value), b(value)];
}
console.log(scopeSeparateMultipliers(2, 3, 4));Question 10: Count a Closure's Calls Completed
Create a closure that records how many times it has been called. Call it count times and return the recorded count.
Expected output: 4 (print this with console.log)
function scopeCountClosureCalls(count) {
// Write your solution here
}
console.log(scopeCountClosureCalls(4));function scopeCountClosureCalls(count) {
let calls = 0;
function record() { calls++; }
for (let i = 0; i < count; i++) record();
return calls;
}
console.log(scopeCountClosureCalls(4));