
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
Palindrome Array in JavaScript
We are required to write a JavaScript function that takes in an array of literals and checks if elements are the same or not if read from front or back i.e. palindrome.
Example
Let’s write the code for this function −
const arr = [1, 5, 7, 4, 15, 4, 7, 5, 1]; const isPalindrome = arr => { const { length: l } = arr; const mid = Math.floor(l / 2); for(let i = 0; i <= mid; i++){ if(arr[i] !== arr[l-i-1]){ return false; }; }; return true; }; console.log(isPalindrome(arr));
Output
The output in the console: −
true
Advertisements