Thursday, 8 March 2012
Palindrome Game by JavaScript
I just created a very simple game which checks whether a word is a palindrome or not.
You can copy and paste the following code in a html file, then open it in a browser.
----------------------------------------------------------------------------------------------
<html>
<head>
<script type="text/javascript">
function validatePalindrome()
{
if(document.getElementById('palindrome').value.indexOf(' ') != -1)
{
alert("Don't enter space");
}
else if(document.getElementById('palindrome').value!="")
{
var palindrome = document.getElementById('palindrome').value;
var x=palindrome;
var w=x.split("");
var z=x.split("");
var notpal=1;
z.reverse();
for(var i=0; i<w.length; i++)
{
if(w[i]!=z[i])
{
alert(palindrome + " is not a palindrome");
var notpal=0;
break;
}
}
if(notpal==1)
alert(palindrome + " is a palindrome");
}
else
{
alert("Enter a word");
}
}
</script>
</head>
<body>
Please enter a word. The word will be checked if it's palindrome or not.<br />
<input id="palindrome" type="text" />
<input onclick="validatePalindrome()" type="button" value="Palindrome" />
</body>
</html>
----------------------------------------------------------------------------------------------
1. Please enter a word
2. Click Palindrome button
Labels:
Javascript,
Web development
Subscribe to:
Post Comments (Atom)


No comments:
Post a Comment