A palindrome is any word, phrase, number, or sequence that reads the exact same way forwards and backwards. Think of it like a mirror for text!
Step 1: What is “Aba”?
Step 2: Read it FORWARDS:
A → B → A = "Aba"
Step 3: Read it BACKWARDS:
A ← B ← A = "Aba" (SAME THING!)
Step 4: Why it works:
“madam” → Reverse = “madam” (Same!)
We can write a Javascript program to check if a given word is a palindrome or not.
function isPalindrome(word) {
// 1. Convert to lowercase (so "Madam" = "madam")
const cleanWord = word.toLowerCase();
// 2. Split into letters → ["m", "a", "d", "a", "m"]
const letters = cleanWord.split('');
// 3. Reverse letters → ["m", "a", "d", "a", "m"] becomes ["m", "a", "d", "a", "m"] backwards? Wait, it's the same!
const reversedLetters = letters.reverse();
// 4. Join reversed letters → "madam"
const reversedWord = reversedLetters.join('');
// 5. Compare: Original vs Reversed
return cleanWord === reversedWord;
} console.log(isPalindrome("Ada")); // true! (Because "ada" reversed = "ada")
console.log(isPalindrome("Aba")); // true! (City in Nigeria)
console.log(isPalindrome("Hannah")); // true!
console.log(isPalindrome("Lagos")); // false ("sogaL" ≠ "Lagos")
console.log(isPalindrome("Abuja")); // false ("ajubA" ≠ "Abuja") function isAdvancedPalindrome(text) {
// Remove spaces/punctuation, keep only letters/numbers
const cleanText = text.toLowerCase().replace(/[^a-z0-9]/g, '');
// Reverse clean text
const reversedText = cleanText.split('').reverse().join('');
return cleanText === reversedText;
}
// Test with phrases:
console.log(isAdvancedPalindrome("Was it a car or a cat I saw")); // true!
console.log(isAdvancedPalindrome("No lemon, no melon")); // true! Latest tech news and coding tips.
1. What Is the Golden Ratio? The Golden Ratio, represented by the Greek letter φ (phi), is…
In CSS, combinators define relationships between selectors. Instead of selecting elements individually, combinators allow you to target elements based…
Below is a comprehensive, beginner-friendly, yet deeply detailed guide to Boolean Algebra, complete with definitions, laws,…
Debugging your own code is hard enough — debugging someone else’s code is a whole…
Git is a free, open-source distributed version control system created by Linus Torvalds.It helps developers: Learn how to…
Bubble Sort is one of the simplest sorting algorithms in computer science. Although it’s not…