JavaScript Notes
Learn to build a palindrome checker in JavaScript with real-time detection, punctuation stripping, case normalization, and visual character highlighting.
What You'll Learn
- How to normalize a string (lowercase, strip non-alphanumeric)
- How to check a palindrome using string reversal
- How to implement an alternative two-pointer approach
- How to give visual feedback by highlighting matching characters
- How to build a history of checked words
Project Overview
A palindrome is a word or phrase that reads the same forwards and backwards (e.g., "racecar", "A man a plan a canal Panama"). This project teaches string manipulation and simple algorithm thinking.
Component Structure
How It Works — Flow Diagram
Step-by-Step Build
Step 1 — HTML Structure
<div class="palindrome-app">
<h1>Palindrome Checker</h1>
<div class="input-row">
<input type="text" id="wordInput" placeholder="Enter a word or phrase..." autocomplete="off">
<button id="checkBtn" class="btn-check">Check</button>
</div>
<div id="result" class="result hidden"></div>
<div id="visualizer" class="visualizer hidden"></div>
<div class="history-section">
<h3>History</h3>
<ul id="historyList" class="history-list"></ul>
<button id="clearHistory" class="btn-clear">Clear History</button>
</div>
</div>Step 2 — Core Logic
Step 3 — Two-Pointer Alternative (Educational)
Step 4 — Visualizer (Character Matching)
Step 5 — Show Result
function check() {
const input = document.getElementById("wordInput").value.trim();
if (!input) return;
const { clean, result } = isPalindrome(input);
if (!clean) {
document.getElementById("result").textContent = "⚠️ No alphanumeric characters found";
document.getElementById("result").className = "result warning";
document.getElementById("result").classList.remove("hidden");
return;
}
const resultEl = document.getElementById("result");
resultEl.classList.remove("hidden");
if (result) {
resultEl.textContent = `✅ "${input}" is a palindrome!`;
resultEl.className = "result success";
} else {
resultEl.textContent = `❌ "${input}" is NOT a palindrome`;
resultEl.className = "result failure";
}
buildVisualizer(clean);
addToHistory(input, result);
}Step 6 — History
Step 7 — Wire Events
Console Output
normalize("racecar") → "racecar"
isPalindrome("racecar")
→ clean = "racecar"
→ reversed = "racecar"
→ true ✅
normalize("A man, a plan, a canal: Panama")
→ "amanaplanacanalpanama"
→ reversed = "amanaplanacanalpanama"
→ true ✅
isPalindrome("hello")
→ clean = "hello"
→ reversed = "olleh"
→ false ❌
isPalindromeTwoPointer("racecar")
→ [r,a,c,e,c,a,r]
→ r===r ✓, a===a ✓, c===c ✓, mid reached → true ✅Edge Cases to Handle
| Input | Expected |
|---|---|
"A man a plan a canal Panama" | ✅ (after normalize) |
"Was it a car or a cat I saw" | ✅ |
"Race a car" | ❌ |
" " | Warning: no alphanumeric chars |
"a" | ✅ (single char is always a palindrome) |
"" | Nothing happens (empty guard) |
Challenge Yourself
- Number palindrome — check if a number reads the same in binary
- Sentence highlighting — mark ignored characters (spaces, punctuation) in grey
- Longest palindrome in a string — find the longest palindromic substring
- Language selection — support non-Latin alphabets with Unicode normalization
- Animated check — animate a visual scan showing character pairs being matched
Best Practices
- Always normalize first —
toLowerCase()and strip punctuation - Write
isPalindrome()as a pure function that takes and returns values (no DOM access) - The
split("").reverse().join("")pattern is idiomatic JavaScript - For large strings, prefer the two-pointer approach — O(n) space, same O(n) time
- Real-time
inputfeedback is great UX but keep the display lightweight to avoid jank
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Build a JavaScript Palindrome Checker - Step by Step Project Tutorial.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, mini, projects, palindrome
Related JavaScript Master Course Topics