Skip to content
Foxipa
Code Completion 8 Challenges

Complete the scope and closure code

Practice lexical scope, shadowing, closures, callbacks, loop bindings, and name resolution by completing real JavaScript code.

Intermediate ~8 min read

Challenge 1

Write the local declaration so the function returns its own label.

javascript Your answer
function label() {
  
  return value;
}

Press Ctrl⌘Enter to check your answer

Challenge 2

Create an inner binding that shadows the outer one.

javascript Your answer
const value = "outer";
function read() {
  
  return value;
}

Press Ctrl⌘Enter to check your answer

Challenge 3

Return a function that reads the surrounding message.

javascript Your answer
function makeReader(message) {
  return () => ;
}

Press Ctrl⌘Enter to check your answer

Challenge 4

Increment the closed-over count and return it.

javascript Your answer
function makeCounter() {
  let count = 0;
  return () => {
    
  };
}

Press Ctrl⌘Enter to check your answer

Challenge 5

Create a formatter that closes over prefix.

javascript Your answer
function makeFormatter(prefix) {
  return (name) => ;
}

Press Ctrl⌘Enter to check your answer

Challenge 6

Read the nearest binding named status.

javascript Your answer
const status = "outer";
function read() {
  const status = "inner";
  return ;
}

Press Ctrl⌘Enter to check your answer

Challenge 7

Use the declaration that gives each for-loop iteration its own binding.

javascript Your answer
for ( i = 0; i < 3; i += 1) {
  readers.push(() => i);
}

Press Ctrl⌘Enter to check your answer

Challenge 8

Read the name property after resolving the user binding.

javascript Your answer
const user = { name: "Ada" };
const result = ;

Press Ctrl⌘Enter to check your answer