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

212
Visualizações
May have a problem with variables being global /local? javascript

can anyone tell me which piece of code is not working as intended and an explanation as to why.

Trying to take any given two integers a and b, which can be positive or negative, find the sum of all the integers between and including them and return it. If the two numbers are equal return a or b.

Note: a and b are not ordered!

More can be found about the question in the link below.

https://www.codewars.com/kata/55f2b110f61eb01779000053/train/javascript


function getSum( a,b ){
  
   let count = 0;
     
    if(a>b){  
      let greaterNum = a;
      let lesserNum = b;
      count = lesserNum;
        for(lesserNum; lesserNum<greaterNum; lesserNum++){
        count += 1;
      }
       return count;
      
    }else if(a<b){
      let greaterNum = b; 
      let lesserNum = a;
      count = lesserNum;
      for(lesserNum; lesserNum<greaterNum; lesserNum++){
        count += 1;
      }
        return count;
    }else if (a==b){
      return a;
    }
  
    
  
  
}

  
  

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

0

find the sum of all the integers between and including them and return it

You're doing:

count += 1;

ie, you're counting the number of numbers - not summing them.

You're also massively overcomplicating it:

function getSum( a,b ){
   const start = Math.min(a,b);
   const end = Math.max(a,b);
   let sum = 0;
   for(let x = start;x<=end;x++)
      sum += x;
   return sum;  
}

console.log(getSum(0,10))
console.log(getSum(10,10))
console.log(getSum(-10,10))
console.log(getSum(10,-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