0
how do you code a palindrome program in JavaScript?
a program to check if a word is the same when reversed
3 Answers
+ 4
function isPalindrome(word){
return word==word.split("").reverse().join("");
}
alert(isPalindrome("mom")) //output true
+ 1
ok thanks, so I tweaked your code to get the result I wanted...
//palindrome checker
function palindrome(word){
var word = prompt("enter word");
if( word.split("").reverse().join("") == word){
alert(" a palindrome");
}
else{
alert("not a palindrome");
}
}
palindrome() //check if the word is same when reversed
0
You reverse the string with Array.prototype.reverse() and compare the two strings.