Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

345
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda