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

538
Vistas
Nested functions is not working but using parameter works

The sumAll function should return the sum of each number between two numbers. If the two numbers is 1 and 4 the output should be 10 since its 1 + 2 + 3 + 4 = 10. I made two nested functions which are least and greater:

  const least = (num1, num2) => {
    if (num1 > num2) {
      return num1;
    } else if (num2 > num1) {
      return num2;
    }
  }

  const greater = (num1, num2) => {
    if (num1 > num2) {
      return num1;
    } else if (num2 > num1) {
      return num2;
    }
  }

least should find which number is the least and greater should find which one is greater.

I called the nested function like this:

  for (let i = least(num1, num2); i <= greater(num1, num2); i++) {
    sum += i;

  }
  return sum;

};

But it didn't work since the output is 4. The i should have the least number in it and in the i <= greater(num1, num2) the greater should have the greater number. It doesn't work when I use the nested functions but it works when I use num1 and num2 like this:

for (let i = num1; i <= num2; i++)

What should I do to make this work using the nested functions?

Here's the fiddle: https://jsfiddle.net/d45v86kf/2/

about 4 years ago · Santiago Gelvez
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