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

343
Visualizações
Special Array: An array is special if every even index contains an even number and every odd index contains an odd number

what is the problem of this code? it's showing false. this should be true.

function isSpecialArray(arr) {
    for(i=0; i<arr.length; i++){
        
        return ((arr[i % 2 == 0]) % 2 == 0) && ((arr[i % 2 !==0]) % 2 !== 0)
    }   
}

console.log(isSpecialArray([2, 7, 4, 9, 6, 1, 6, 3])) // false??
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can simplify your code and avoid unnecessary looping as soon as the first element breaking the rule is found.

Using a for loop

function isSpecial(arr) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] % 2 !== i % 2) return false
  }
  return true;
}

Using Array.prototype.every

function isSpecial(arr) {
  return arr.every((item, index) => item % 2 === index % 2);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Your return ((arr[i % 2 == 0]) % 2 == 0) && ((arr[i % 2 !==0]) % 2 !== 0) syntax is wrong. Hope the below function meets your requirement.

Logic

  • Loop through the array, check each node for "special" condition and store it in isNodeSpecial
  • "special" condition means, even index has even number and odd index have odd number.
  • Initialize a variable outside the loop which holds the isSpecial status of array.
  • Update the isSpecial varaible as logical and of isSpecial and isNodeSpecial.
  • Whenever a single node violates the condition the isSpecial is set to false and loop exits.

function isSpecialArray(arr) {
  let isSpecial = true;
  for (i = 0; i < arr.length && isSpecial; i++) {
    const isNodeSpecial = (i % 2 === 0) ? arr[i] % 2 === 0 : arr[i] % 2 === 1;
    isSpecial = isSpecial && isNodeSpecial;
  }
  return isSpecial;
}

console.log(isSpecialArray([2, 7, 4, 9, 6, 1, 6, 3])); // true
console.log(isSpecialArray([2, 7, 4, 10, 6, 1, 6, 3]));// false

about 4 years ago · Juan Pablo Isaza Relatório

0

You can check if any element does not satisfy the condition and immediately return false. If all elements satisfy the condition, then return true. That is best solution when it comes to performance. You can change your code like this:

function isSpecialArray(arr) {
  for (i = 0; i < arr.length; i++) {
    if (arr[i] % 2 !== i % 2) return false;
  }
  return true;
}

console.log(isSpecialArray([2, 7, 4, 9, 6, 1, 6, 3])) //true
console.log(isSpecialArray([1, 7, 4, 9, 6, 1, 6, 3])) //false

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