Palindrome Check
Program to check if a number or string is palindrome
JavaScript Palindrome Check Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// Palindrome: reads same forwards and backwards
// Examples: 121, "madam", "racecar"
// Method 1: String palindrome
function isPalindromeString(str) {
str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
let reversed = str.split('').reverse().join('');
return str === reversed;
}
console.log("madam:", isPalindromeString("madam"));
console.log("hello:", isPalindromeString("hello"));
console.log("A man a plan a canal Panama:", isPalindromeString("A man a plan a canal Panama"));
// Method 2: Number palindrome
function isPalindromeNumber(num) {
let original = num;
let reversed = 0;
while (num > 0) {
reversed = reversed * 10 + (num % 10);
num = Math.floor(num / 10);
}
return original === reversed;
}
console.log("\nNumber palindrome:");
console.log("121:", isPalindromeNumber(121));
console.log("123:", isPalindromeNumber(123));
console.log("1221:", isPalindromeNumber(1221));
// Method 3: Two-pointer approach (efficient)
function isPalindromeTwoPointer(str) {
str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
let left = 0;
let right = str.length - 1;
while (left < right) {
if (str[left] !== str[right]) {
return false;
}
left++;
right--;
}
return true;
}
console.log("\nTwo-pointer approach:");
console.log("racecar:", isPalindromeTwoPointer("racecar"));
console.log("hello:", isPalindromeTwoPointer("hello"));
// Method 4: Recursive approach
function isPalindromeRecursive(str) {
str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
if (str.length <= 1) return true;
if (str[0] !== str[str.length - 1]) return false;
return isPalindromeRecursive(str.slice(1, -1));
}
console.log("\nRecursive approach:");
console.log("level:", isPalindromeRecursive("level"));
console.log("world:", isPalindromeRecursive("world"));madam: true hello: false A man a plan a canal Panama: true Number palindrome: 121: true 123: false 1221: true Two-pointer approach: racecar: true hello: false Recursive approach: level: true world: false
Understanding Palindrome Check
This program demonstrates different methods to check if a value is a palindrome.
Palindrome Definition
A palindrome reads the same forwards and backwards:
Method 1: String Reversal
Compare original with reversed:
let reversed = str.split('').reverse().join('');
return str === reversed;String Cleaning:
toLowerCase(): Case-insensitivereplace(/[^a-z0-9]/g, ''): Remove non-alphanumericMethod 2: Number Palindrome
Reverse number and compare:
let reversed = 0;
while (num > 0) {
reversed = reversed * 10 + (num % 10);
num = Math.floor(num / 10);
}
return original === reversed;Method 3: Two-Pointer Approach
Compare characters from both ends:
let left = 0;
let right = str.length - 1;
while (left < right) {
if (str[left] !== str[right]) return false;
left++;
right--;
}
return true;Pros:
Method 4: Recursive
Check first and last, recurse on middle:
if (str[0] !== str[str.length - 1]) return false;
return isPalindromeRecursive(str.slice(1, -1));String Methods:
slice(1, -1): Remove first and last charactercharAt(0): Get first characterlength: String lengthTime Complexity:
When to Use:
-
Reversal
: Simple, readable
-
Two-pointer
: Most efficient
-
Recursive
: Learning recursion
Let us now understand every line and the components of the above program.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.