🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
A palindrome is a word, phrase, or sequence of characters that reads the same backward as forward, ignoring spaces, punctuation, and capitalization. Common examples of palindromes include "madam", "racecar", and "121". This program will help you check whether a given string is a palindrome using JavaScript.
Problem Statement
Create a JavaScript program that:
- Accepts a string.
- Checks if the string is a palindrome.
- Returns
trueif the string is a palindrome andfalseotherwise.
Example:
Input:
"madam"Output:
madam is a palindromeInput:
"hello"Output:
hello is not a palindrome
Solution Steps
- Read the Input String: Provide the string either as user input or directly in the code.
- Normalize the String: Convert the string to lowercase and remove non-alphanumeric characters for accurate palindrome checks.
- Check if the String is a Palindrome: Reverse the string and compare it with the original normalized string.
- Display the Result: Print whether the string is a palindrome or not.
JavaScript Program
// JavaScript Program to Check if a String is a Palindrome
// Author: https://www.javaguides.net/
function isPalindrome(str) {
// Step 1: Normalize the string (convert to lowercase and remove non-alphanumeric characters)
let normalizedStr = str.toLowerCase().replace(/[^a-z0-9]/g, '');
// Step 2: Reverse the string
let reversedStr = normalizedStr.split('').reverse().join('');
// Step 3: Check if the original string and reversed string are the same
return normalizedStr === reversedStr;
}
// Example input
let inputString = "madam";
if (isPalindrome(inputString)) {
console.log(`${inputString} is a palindrome`);
} else {
console.log(`${inputString} is not a palindrome`);
}
Output
madam is a palindrome
Example with Different Input
let inputString = "hello";
if (isPalindrome(inputString)) {
console.log(`${inputString} is a palindrome`);
} else {
console.log(`${inputString} is not a palindrome`);
Output:
hello is not a palindrome
Explanation
Step 1: Normalize the String
- Convert the string to lowercase using
toLowerCase()and remove any non-alphanumeric characters (spaces, punctuation, etc.) using a regular expression (replace(/[^a-z0-9]/g, '')). This ensures accurate comparison.
Step 2: Reverse the String
- Use
split('')to convert the string into an array of characters, reverse the array usingreverse(), and then join the characters back into a string usingjoin('').
Step 3: Compare the Original and Reversed String
- Compare the normalized string with its reversed version. If they are equal, the string is a palindrome.
Step 4: Display the Result
- If the string is a palindrome, print the result accordingly using
console.log().
Conclusion
This JavaScript program checks whether a given string is a palindrome by normalizing the string, reversing it, and comparing the two versions. Palindrome checking is a common task in many programming problems, and this approach ensures that the string is correctly compared, even when it contains non-alphanumeric characters.
Comments
Post a Comment
Leave Comment