Scopes - Exercise 2

Question 1: Capture an Addition Amount

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)

javascript

function scopeCaptureAddition(increment, value) {
  // Write your solution here
}

console.log(scopeCaptureAddition(3, 7));

Question 2: Call a Counter Three Times

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)

javascript

function scopeThreeCounterCalls(start, step) {
  // Write your solution here
}

console.log(scopeThreeCounterCalls(2, 3));

Question 3: Create Independent Counters

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)

javascript

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

console.log(scopeIndependentCounters(1, 10));

Question 4: Capture a Prefix in a Closure

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)

javascript

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

console.log(scopePrefixTwoWords("pre-", "one", "two"));

Question 5: Read a Changed Captured Value

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)

javascript

function scopeReadChangedCapture(initial, replacement) {
  // Write your solution here
}

console.log(scopeReadChangedCapture("old", "new"));

Question 6: Accumulate Closure Deposits

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)

javascript

function scopeAccumulateDeposits(start, deposits) {
  // Write your solution here
}

console.log(scopeAccumulateDeposits(10, [2,3]));

Question 7: Capture a Private Name

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)

javascript

function scopePrivateNameGreeting(name, greeting) {
  // Write your solution here
}

console.log(scopePrivateNameGreeting("Ava", "Hello"));

Question 8: Update a Captured Setting

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)

javascript

function scopeSharedSetting(initial, next) {
  // Write your solution here
}

console.log(scopeSharedSetting("off", "on"));

Question 9: Capture Separate Multipliers

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)

javascript

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

console.log(scopeSeparateMultipliers(2, 3, 4));

Question 10: Count a Closure's Calls

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)

javascript

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

console.log(scopeCountClosureCalls(4));