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

340
Visualizações
Function isSpecialNumber(n) doesn't give the desired result

function isSpecialNumber(n) {
  let str = n.toString();

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

    if (str.charAt(i) > 5) {
      return 'NOT!!'
    } else {
      return 'Special!!';
    }
  }

}

console.log(
  isSpecialNumber(144525),
  isSpecialNumber(2),
  isSpecialNumber(9),
  isSpecialNumber(23),
  isSpecialNumber(39)
)

How it have to work:

isSpecialNumber(2) === 'Special!!' 2 is a single digit in the interval [0:5].

isSpecialNumber(9) === 'NOT!!' 9 is a one-digit number, but it is not in the interval [0:5].

isSpecialNumber(23) === 'Special!!' All digits of the number 23 are in the interval [0:5].

isSpecialNumber(39) === 'NOT!!' The second digit (9) is not in the interval [0:5].

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

0

The only thing you need to do is put the return ¨Special¨ outside of the for. Like this:

function isSpecialNumber(n) {
  let str = n.toString();

  for (let i = 0; i < str.length; i++) {
    if (str.charAt(i) > 5) {
      return "NOT!!";
    } 
  }
  return "Special!!"
}

Because in your solution if one of the numbers is smaller than 5 its returning "Special" and maybe the next one is not.

about 4 years ago · Juan Pablo Isaza Relatório

0

function isSpecialNumber(n) {
  let str = n.toString();
  let found = 'special'; 

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

    if (str.charAt(i) > 5) {
      found =  'NOT!!';
    }
  }
  return found;

}

console.log(
  isSpecialNumber(144525),
  isSpecialNumber(2),
  isSpecialNumber(9),
  isSpecialNumber(23),
  isSpecialNumber(39)
)
about 4 years ago · Juan Pablo Isaza Relatório

0

Try this, it's a little bit different but it should do the same:

function isSpecialNumber(n) {
  // n.toString() turns the number into a string
  // .split('') splits the string into an array
  // .every loops through the items in an array and
  // checks that all the elements return true for the function that
  // passed as the parameter into the every function.
  // .every doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
  const special = n.toString()
    .split()
    .every((digit) => parseInt(digit) < 5)

  // You can also do this, which uses .some
  // .some doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
  /*
  const special = n.toString()
    .split()
    .some((digit) => parseInt(digit) > 5)
  */
  
  // if all the digits are less than 5, then it's special
  // and should return "Special!!"
  // else, return "NOT!!"
  return special ? "Special!!" : "NOT!!"
}

console.log(
  isSpecialNumber(144525),
  isSpecialNumber(2),
  isSpecialNumber(9),
  isSpecialNumber(23),
  isSpecialNumber(39)
)
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