Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
How to write a function that returns an array of functions?

Objective

Write a function values(f, low, high) that yields an array of function values [f(low), f(low + 1), . . ., f(high)].

What I Tried

function values(f, low, high) {
  var result = [];

  for(var i = low; i < high; i++) {
    result.push(j = f(i));
  }

  return result;
}

const vf = (p1) => console.log(p1);

const arrayOfFuns = values(vf, 10, 16);

console.log(arrayOfFuns);
console.log(arrayOfFuns[0]());

Expected Output

10
11
12
13
14
15
[ function, function, function, function, function, function ]
10 // console.log(arrayOfFuns[0]());

Output

10
11
12
13
14
15
[ undefined, undefined, undefined, undefined, undefined, undefined ]
/home/moss/var/Modern JS for the Impatient/3.3.js:16
console.log(arrayOfFuns[0]());
                          ^

TypeError: arrayOfFuns[0] is not a function
    at Object.<anonymous> (/home/moss/var/Modern JS for the Impatient/3.3.js:16:27)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47

Why is the arrayOfFuns array element filled with undefined? I thought it would be an array of functions. Also I expected that accessing the first element of arrayOfFuns would print 10.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I think you are looking for something like this.

I have created a function named nf which returns the ith value and pushed it in the array.

function values(f, low, high) {
  var result = [];

  for(let i = low; i < high; i++) {
    f(i);
    const nf = () => i;
    result.push(nf);
  }

  return result;
}

const vf = (p1) => console.log(p1);

const arrayOfFuns = values(vf, 10, 16);

console.log(arrayOfFuns);
console.log(arrayOfFuns[0]());

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can create an anonymous function each time.

function values(f, low, high) {
  var result = [];

  for(var i = low; i < high; i++) {
    result.push(()=> f(i));
  }

  return result;
}

const vf = (p1) => console.log(p1);

const arrayOfFuns = values(vf, 10, 16);

console.log(arrayOfFuns);
console.log(arrayOfFuns[0]());

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda