Finding special array - JavaScript


An array is a special array if −

--All the elements at odd indices are odd.
--All the elements at even indices are even.

We are required to write a JavaScript function that takes in an array and checks if its a special array or not.

Example

Following is the code −

const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const isSpecial = (arr = []) => {
   for(let i = 0; i < arr.length; i++){
      if(arr[i] % 2 === i % 2){
         continue;
      };
      return false;
   };
   return true;
};
console.log(isSpecial(arr));

Output

Following is the output in the console −

true

Updated on: 16-Sep-2020

297 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements