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

178
Vistas
'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 Respuestas
Responde la pregunta

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 Denunciar

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 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