Scopes - Exercise 5

Question 1: Create a Private List

Create a local array and an add function that appends each input item. Return a copy of the list after all items are added.

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

javascript

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

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

Question 2: Create a Private Total

Create a local total and a function that adds each input amount. Return the final total.

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

javascript

function scopePrivateTotal(amounts) {
  // Write your solution here
}

console.log(scopePrivateTotal([2,5,-1]));

Question 3: Change a Captured Object

Capture a local object in a reader, update its value property, then return what the reader sees.

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

javascript

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

console.log(scopeCapturedObjectUpdate(1, 7));

Question 4: Replace a Captured Object

Capture a local object variable in a reader, reassign it to a new object with the supplied replacement value, and return what the reader sees.

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

javascript

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

console.log(scopeCapturedObjectReassignment(1, 7));

Question 5: Return a Closure's Two States

Create a local value and a reader. Record the reader's result, update the value, then return the old and new results.

Expected output: ["off","on"] (print this with console.log)

javascript

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

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

Question 6: Create a Private Toggle

Create a closure with an initial boolean. Each call flips it. Call it twice and return both results.

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

javascript

function scopePrivateToggle(initial) {
  // Write your solution here
}

console.log(scopePrivateToggle(false));

Question 7: Capture a Tax Rate

Create a closure that calculates price times the captured rate. Return the tax for each supplied price.

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

javascript

function scopeCapturedTaxRate(rate, prices) {
  // Write your solution here
}

console.log(scopeCapturedTaxRate(0.5, [4,8]));

Question 8: Keep Independent Private Lists

Create two separate closures, each holding an array. Add first to one and second to the other, then return both lists.

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

javascript

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

console.log(scopeIndependentPrivateLists("a", "b"));

Question 9: Capture a Starting Offset

Create a closure that adds the supplied offset to its input, then use it for each array value.

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

javascript

function scopeMapWithOffset(offset, values) {
  // Write your solution here
}

console.log(scopeMapWithOffset(5, [1,2]));

Question 10: Count Values Above a Captured Limit

Create a predicate closing over the limit and return the number of array values greater than it.

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

javascript

function scopeCountAboveCapturedLimit(limit, values) {
  // Write your solution here
}

console.log(scopeCountAboveCapturedLimit(3, [1,4,5]));