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

536
Visualizações
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 à 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