cf.completefrontend

Write the codeEasy

Fix createHandlers so each returned function reports its own index and name.

This version returns functions that all give "3: undefined" when you pass three names. Rewrite it so the function at index i returns "i: name", for example "0: save".

function createHandlers(names) {
  var handlers = [];
  for (var i = 0; i < names.length; i++) {
    handlers.push(function () {
      return i + ": " + names[i];
    });
  }
  return handlers;
}
Write this
function createHandlers(names) {
  // your code
}
 
// const [first, second] = createHandlers(["save", "load"]);
// first();  // "0: save"
// second(); // "1: load"

The answer

Write it yourself first. Then compare it with a reference solution and the checks it passes.

Checking your sign-in…