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

220
Vistas
How can I pass this function to another function to use the iterator of the top function?

So I wanted to replicate the 𝝨 function/operation in Javascript.

For the people not familiar with it, here is an example:

Capital sigma

The above displayed operation would translate to Javascript code like this:

var sum = 0;
for (var n = 1; n <= 10; n++) {
    sum += n ** 2;
}

Now I wanted to wrap it as a function, where the operation that each iteration should do would be passed as a function. So the example above would be passed like this:

𝝨(1, 10, n => n ** 2)
function 𝝨(start, end, func) {
    var sum = 0;
    for (var i = start; i <= end; i++) {
        sum += func();
    }
    return sum;
}

However, the above example returns NaN because the variable passed doesn't use the iterator of the for-loop. Is there a way to do that?

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

0

You're not passing in the iterative value to the callback func:

    for (var i = start; i <= end; i++) {
        sum += func(i); // you have to pass it in so func knows how to func
    }
about 4 years ago · Juan Pablo Isaza Denunciar

0

But why do we want func()? This works fine too I guess. It outputs the same answer 385.

function 𝝨(start, end){
  let sum = 0;
for (let n = start; n <= end; n++) {
    sum += n ** 2;
}
return sum;
}
alert(𝝨(1, 10));

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