Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

340
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!