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

477
Visualizações
Returning Null - why is this going wrong?

I am returning null with this code, however I cannot see why/where this is going wrong. (It may be due to the sort function since I got this from a well upvoted snippet on how to sort an array). I care more about the understanding than the correct answer.

Task I am trying to achieve :

Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having an non-negative integer size. Since he likes to make things perfect, he wants to arrange them from smallest to largest so that each statue will be bigger than the previous one exactly by 1. He may need some additional statues to be able to accomplish that. Help him figure out the minimum number of additional statues needed.

Example

For statues = [6, 2, 3, 8], the output should be solution(statues) = 3.

Ratiorg needs statues of sizes 4, 5 and 7.

My code & thinking:

function solution(statues) {

  let total = 0;

  statues.sort(function(a, b) { //From what I understand this should sort the array numerically 
    return a - b;
  });

  for (let i = 1; i < statues.length; i++) { //iterate through the array comparing the index to the one before it
    if (statues[i + 1] != (statues[i] + 1)) { //if there is a diff between index of more than 1 it will add the diff 
      total += statues[i + 1] - statues[i]; //to the total variable 
    }
  }
  return total;

}

const result = solution([6, 2, 3, 8]);
console.log(result);

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Thanks to people who commented who helped me see that there were some errors in my logic - I have now changed the if statement to compare an index starting at 1 to the behind it i.e index 1 compared to index 0. On top of that, there was an issue with the following line total += statues[i] - statues[i-1]; I replaced that with a for loop so that it counts up to the number rather than a simple subtraction of it.


 function solution(statues) {
        //[6, 2, 3, 8] --- 2,3,6,8  --- 3,6 7-2, 
        let total = 0;
        
        statues.sort(function(a, b) {                  
            return a - b;
                });
       
        for(let i =1; i<statues.length; i++){         
            if(statues[i] != (statues[i-1]+1) ){       
                //total += statues[i] - statues[i-1];    
                for(let j = statues[i-1]; j<statues[i]-1; j++){
                    total += 1;
                }
                 } 
                 
        }
        return total;
        
    }

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