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

175
Visualizações
'For loop' within a function

I hope everyone is having an amazing day so far.

I am asking for help on an exercise that I am stuck on. I have gone through and researched all over but still am not getting it right.

The task is:

Create a for loop in which an iterator starting from 0 up to the iteratorMax value (not included) (the second parameter to your function) is incremented by 1. Add the iterator to num(the first parameter to your function) for each incrementation. you must return the number at the end of your function.

I was given this code to start with:

    function IncrementNumber(num, iteratorMax){
        //Add For loop here
    //return value here
    }

I have tried coding it a number of different ways but still not correct.

for example:

    function IncrementNumber(num, iteratorMax){
    for (let i = 0; i < iteratorMax; i++) {
            return(i);
        }

    }

I have tried to declare "i" but that seems to be incorrect also.

I really appreciate any help or hints to what I am missing or doing wrong in my code.

Thank you!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

For the IncrementNumber(value, N) method, when value is 0, this algorithm returns the result of the equation:

Total = N x (N-1) / 2

function IncrementNumber(num, iteratorMax){
  for(let i = 0 ; i < iteratorMax ; ++i){
      num += i;
      console.log(`i: ${i}\tnumber: ${num}`);
  }
  return num;
}

console.log(`Result: ${IncrementNumber(0, 10)}`);

about 4 years ago · Juan Pablo Isaza Relatório

0

If you call return within the loop, the loop will only be executed once. You need to move the return statement outside of the loop. Whenever calling return, this will cause the current function to exit.

function IncrementNumber(num, iteratorMax){
  for (let i = 0; i < iteratorMax; i++) {
    num += i;
  }
  
  return num;
}
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