Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda