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

198
Visualizações
javascript query: somehow javascript is finding length of an integer

unsorted = [2,4,88,5,6,5,5,8888888]
unsorted = unsorted.sort(function(a,b){
   if (a.length == b.length) {
        if (a < b) {
            return -1;
        } else if (a > b) {
            return 1;
        } else {
            return 0;
        }
        
    } else if (a.length < b.length) {
        return -1;
    } else {
        return 1;
    }
});
console.log(unsorted);
explain the compare function used to sort array in ascending order and while using compare function as (a,b)=>a-b, some of the test cases didn't pass.

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

0

Numbers do not have a length property, so 1.length is undefined. Since a.length and b.length are both undefined, a.length == b.length returns true.

If you'd actually like to compare the length of digits in a number, first convert it to a string: a.toString().length

var a=1;
var b=10;
console.log("a.length: " + a.length);
console.log("b.length: " + b.length);
console.log(a.length == b.length);


console.log("a.toString().length: " + a.toString().length);
console.log("b.toString().length: " + b.toString().length);
console.log(a.toString().length == b.toString().length);

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