No need to use arrays. Not the best way to do this, but for simple words it will work.
Note: No checks for punctuation or spaces if used to check for palindrome phrases.
Code:
<html>
<head>
<title>Palindrome Check</title>
</head>
<body>
<script type="text/javascript">
// From: http://www.webdeveloper.com/forum/showthread.php?t=226128
//declare s1 as a variable and prompt user for a word
var s1 = prompt("enter a word");
// reverses the word entered by the user
function reverseWord(s1) {
s1 = s1.toUpperCase() // make word upper case
var s2 = '';
for (var i = s1.length - 1; i>=0; i--) {
s2 += s1[i]; // take word that was reversed and put it in s2
}
return s2;
}
/* INVALID ARRAY ASSIGNMENT
// turn them into an array
var arrays = new array();
var array1[] = s1;
var array2[] = s2;
*/
// compare the words and display message
if(s1.toUpperCase() == reverseWord(s1)) {
alert( s1 + " is a palindrome");
} else {
alert( s1 + " is not a palindrome");
}
// compare the words and display a message
//if (s1 == s2)
//alert( s1 + " is a palindrome");
//else
// alert( s1 + " is not a palindrome");
</script>
</body>
</html>
Note: remove extra comments from original post after you see why it was not needed.
BTW: Please use [ code] and [ /code] tags (without the spaces)
to make it easier to read you code in the forum.
Bookmarks