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

87
Visualizações
Function iisTidy(number) doesn't give the desired result

function isTidy(number) {

  let str = number.toString();

  for (let i = 1; i < str.length; i++) {

    if (str.charAt(i) < str.charAt(i + 1)) {
      return true;
    }
    if (str.charAt(i) > str.charAt(i + 1)) {
      return false;
    }
    if (str.charAt(i) == str.charAt(i + 1)) {
      return false;
    }
  }
}

console.log(isTidy(135587));

How it have to work:

isTidy(12) ==> true

The numbers { 1, 2 } are in non-decreasing sequence, i.e. 1 <= 2.

isTidy(32) ==> false

The numbers { 3, 2 } are in descending order, that is, 3 > 2.

isTidy(1024) ==> false

The numbers { 1, 0, 2, 4 } are in descending order because 0 < 1.

isTidy(3445) ==> true

The numbers { 3, 4, 4, 5 } are in non-decreasing sequence, because 4 <= 4.

isTidy(13579) ==> true

The numbers { 1, 3, 5, 7, 9} are in ascending order.

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

0

You need to check the previous digit as well and exit early if the number are not equal or increasing.

At the end return true.

function isTidy(number) {
    let str = number.toString();
    for (let i = 1; i < str.length; i++) {
        if (str[i - 1] > str[i]) return false;
    }
    return true;
}

console.log(isTidy(135578));
console.log(isTidy(135587));

about 4 years ago · Juan Pablo Isaza Relatório

0

function isTidy(number) {
  let str = number.toString();
  for (let i = 1; i < str.length; i++) {
    let nextNumber = str.charAt(i + 1);
    if (nextNumber != "" && str.charAt(i) > nextNumber) return false;
  }
  return true;
}

console.log(isTidy(135587));

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