Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
¿Cómo escribir una función que devuelva una matriz de funciones?

Objetivo

Escriba una función de values(f, low, high) que produzca una matriz de valores de función [f(low), f(low + 1), . . ., f(high) ].

lo que probé

 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]());

Rendimiento esperado

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

Producción

 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

¿Por qué el elemento de matriz arrayOfFuns está lleno de undefined ? Pensé que sería una serie de funciones. También esperaba que acceder al primer elemento de arrayOfFuns imprimiera 10 .

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Creo que estás buscando algo como esto.

Creé una función llamada nf que devuelve el i-ésimo valor y lo empujé en la matriz.

 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 Report

0

Puede crear una función anónima cada vez.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!