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

77
Visualizações
Each digit differs from the next one by 1

Script has to work this way:

isJumping(9) === 'JUMPING'

This is a one digit number

isJumping(79) === 'NOT JUMPING'

Neighboring digits do not differ by 1

isJumping(23454) === 'JUMPING'

Neighboring digits differ by 1

I have:

function isJumping(number) {
    let str = number.toString();
    for (let i = 1; i < str.length; i++) {
        if (Math.abs(str[i+1]) - Math.abs(str[i]) == 1){
            return 'JUMPING';
        }
    }
    return 'NOT JUMPING';
}
console.log(isJumping(345));

Help please, where is mistake?

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

0

There are a couple of issues:

  1. You're not handling single digits.
  2. You're returning too early. You're returning the first time you see a difference of 1 between digits, but you don't know that subsequent differences will also be 1.
  3. You're not checking the difference between the first and second digits, and you're going past the end of the string.
  4. You're using Math.abs as a means of converting digits to numbers.

Instead (see comments):

function isJumping(number) {
    let str = number.toString();
    for (let i = 1; i < str.length; i++) {
        // Convert to number, do the difference, then
        // use Math.abs to make -1 into 1 if necessary
        if (Math.abs(+str[i] - str[i-1]) !== 1) {
            // Found a difference != 1, so we're not jumping
            return "NOT JUMPING";
        }
    }
    // Never found a difference != 1, so we're jumping
    return "JUMPING";
}
console.log(isJumping(345));    // JUMPING
console.log(isJumping(9));      // JUMPING
console.log(isJumping(79));     // NOT JUMPING
console.log(isJumping(23454));  // JUMPING

In that, I use +str[i] to convert str[i] to number and implicitly convert str[i-1] to number via the - operator, but there are lots of ways to convert strings to numbers (I list them here), pick the one that makes sense for your use case.

You might also need to allow for negative numbers (isJumping(-23)).

about 4 years ago · Juan Pablo Isaza Relatório

0

Loop over the characters and early return with "NOT JUMPING" if the condition is violated & if the condition is never violated return "JUMPING".

function isJumping(num) {
  const strNum = String(Math.abs(num));

  for (let i = 0; i < strNum.length - 1; i++) {
    if (Math.abs(strNum[i] - strNum[i + 1]) > 1) {
      // replace `> 1` with `!== 1`, if diff 0 is not valid!
      return "NOT JUMPING";
    }
  }

  return "JUMPING";
}

console.log(isJumping(9));
console.log(isJumping(79));
console.log(isJumping(23454));

about 4 years ago · Juan Pablo Isaza Relatório

0

A clumsy way would be if (Math.abs(Math.abs(str[i+1]) - Math.abs(str[i])) == 1). Right now you are using Math.abs() to convert digits to numbers. Also, indexing is off, you start from 1, which is good, but then you should compare [i] and [i-1]. And the usual mismatch: you can say "JUMPING", only at the end. So you should check for !==1, and return "NOT JUMPING" inside the loop, and "JUMPING" after. That would handle the 1-digit case too.

It's a more readable practice to use parseInt() for making a number from a digit, otherwise the implementation of the comment:

function isJumping(number) {
    let str = number.toString();
    for (let i = 1; i < str.length; i++) {
        if (Math.abs(parseInt(str[i-1]) - parseInt(str[i])) !== 1){
            return 'NOT JUMPING';
        }
    }
    return 'JUMPING';
}
console.log(isJumping(345));
console.log(isJumping(3));
console.log(isJumping(79));

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