Scopes - Exercise 5
Question 1: Create a Private List Completed
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)
function scopePrivateListCopy(items) {
// Write your solution here
}
console.log(scopePrivateListCopy(["a","b"]));function scopePrivateListCopy(items) {
const list = [];
function add(item) { list.push(item); }
for (const item of items) add(item);
return [...list];
}
console.log(scopePrivateListCopy(["a","b"]));Question 2: Create a Private Total Completed
Create a local total and a function that adds each input amount. Return the final total.
Expected output: 6 (print this with console.log)
function scopePrivateTotal(amounts) {
// Write your solution here
}
console.log(scopePrivateTotal([2,5,-1]));function scopePrivateTotal(amounts) {
let total = 0;
function add(amount) { total += amount; }
for (const amount of amounts) add(amount);
return total;
}
console.log(scopePrivateTotal([2,5,-1]));Question 3: Change a Captured Object Completed
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)
function scopeCapturedObjectUpdate(initial, replacement) {
// Write your solution here
}
console.log(scopeCapturedObjectUpdate(1, 7));function scopeCapturedObjectUpdate(initial, replacement) {
const state = { value: initial };
function read() { return state.value; }
state.value = replacement;
return read();
}
console.log(scopeCapturedObjectUpdate(1, 7));Question 4: Replace a Captured Object Completed
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)
function scopeCapturedObjectReassignment(initial, replacement) {
// Write your solution here
}
console.log(scopeCapturedObjectReassignment(1, 7));function scopeCapturedObjectReassignment(initial, replacement) {
let state = { value: initial };
function read() { return state.value; }
state = { value: replacement };
return read();
}
console.log(scopeCapturedObjectReassignment(1, 7));Question 5: Return a Closure's Two States Completed
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)
function scopeReadBeforeAndAfter(initial, replacement) {
// Write your solution here
}
console.log(scopeReadBeforeAndAfter("off", "on"));function scopeReadBeforeAndAfter(initial, replacement) {
let value = initial;
function read() { return value; }
const before = read();
value = replacement;
return [before, read()];
}
console.log(scopeReadBeforeAndAfter("off", "on"));Question 6: Create a Private Toggle Completed
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)
function scopePrivateToggle(initial) {
// Write your solution here
}
console.log(scopePrivateToggle(false));function scopePrivateToggle(initial) {
let value = initial;
function toggle() { value = !value; return value; }
return [toggle(), toggle()];
}
console.log(scopePrivateToggle(false));Question 7: Capture a Tax Rate Completed
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)
function scopeCapturedTaxRate(rate, prices) {
// Write your solution here
}
console.log(scopeCapturedTaxRate(0.5, [4,8]));function scopeCapturedTaxRate(rate, prices) {
function tax(price) { return price * rate; }
return prices.map(tax);
}
console.log(scopeCapturedTaxRate(0.5, [4,8]));Question 8: Keep Independent Private Lists Completed
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)
function scopeIndependentPrivateLists(first, second) {
// Write your solution here
}
console.log(scopeIndependentPrivateLists("a", "b"));function scopeIndependentPrivateLists(first, second) {
function makeList() { const list = []; return (item) => { list.push(item); return [...list]; }; }
const a = makeList();
const b = makeList();
return [a(first), b(second)];
}
console.log(scopeIndependentPrivateLists("a", "b"));Question 9: Capture a Starting Offset Completed
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)
function scopeMapWithOffset(offset, values) {
// Write your solution here
}
console.log(scopeMapWithOffset(5, [1,2]));function scopeMapWithOffset(offset, values) {
function addOffset(value) { return value + offset; }
return values.map(addOffset);
}
console.log(scopeMapWithOffset(5, [1,2]));Question 10: Count Values Above a Captured Limit Completed
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)
function scopeCountAboveCapturedLimit(limit, values) {
// Write your solution here
}
console.log(scopeCountAboveCapturedLimit(3, [1,4,5]));function scopeCountAboveCapturedLimit(limit, values) {
function above(value) { return value > limit; }
return values.filter(above).length;
}
console.log(scopeCountAboveCapturedLimit(3, [1,4,5]));