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

133
Views
Funciones de orden superior y devoluciones de llamada: uso de "retorno"
function eitherCallback(callback1, callback2) { return (element, i, array) => {return callback1(element, i, array) || callback2(element, i, array)} } function filterArray(array, callback) { const newArray = []; for (let i = 0; i < array.length; i += 1) { if (callback(array[i], i, array)) newArray.push(array[i]); } return newArray; } const arrOfNums = [10, 35, 105, 9]; const integerSquareRoot = n => Math.sqrt(n) % 1 === 0; // condition 1 as a function const over100 = n => n > 100; // condition 2 as a function const intSqRtOrOver100 = eitherCallback(integerSquareRoot, over100); // function to combine functions console.log(filterArray(arrOfNums, intSqRtOrOver100)); // should log: [105, 9]

Arriba hay un fragmento de código que he escrito para mi preparación de bootcamp de codificación. Sin embargo, estoy confundido sobre el propósito del segundo "retorno" en la función de devolución de llamada. He leído la sintaxis de las funciones de flecha en ES6 y leí que el retorno no es necesario si la función es una sola línea. Sin embargo, cuando elimino ese segundo "retorno" la salida de console.log(filterArray(arrOfNums, intSqRtOrOver100)); devuelve una matriz vacía.

La función de devolución de llamada toma dos argumentos: devolución de llamada1 y devolución de llamada2, que serán funciones integerSquareRoot y over100. Cuando miro esas funciones, el retorno de esas dos funciones son booleanos, lo que me lleva a mi confusión. Dado que callback1 y callback2 ya devuelven valores booleanos, ¿por qué obtengo una matriz vacía si omito el segundo "retorno" en la función de devolución de llamada?

Gracias.

about 4 years ago · Juan Pablo Isaza
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!