
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding Special Array in 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
Advertisements