Write a JavaScript program to check palindrome string
Description: Check if the words “madam”, “Level” and “hello” are palindromes.
Hint:
- A palindrome reads the same backwards.
- Change the text to lowercase first.
JavaScript palindrome program
function isPalindrome(text) {
const clean = text.toLowerCase();
return clean === clean.split("").reverse().join("");
}
for (const w of ["madam", "Level", "hello"]) {
console.log(w + (isPalindrome(w) ? " is a palindrome" : " is not a palindrome"));
}
Output:
Tutorials Class - Output Window
madam is a palindrome
Level is a palindrome
hello is not a palindrome
How it works
toLowerCase()makes “Level” equal to “level”.- The text is compared with its reverse.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |