50. JavaScript Program to Check given String is Palindrome or Not.
How does this program work?
- This program is used to check given string is palindrome or not using JavaScript.
- Palindrome String is a string which means if it remains same even after reversing the given string.
- A string said to be a palindrome if original string is equal to the reverse of the given string otherwise it is not a palindrome.
Here is the code
<html>
<head>
<title>JavaScript program to check given string is palindrome or not</title>
</head>
<body>
<table>
<tr>
<td> <input type="text" name="a" id="first" placeholder="Enter string"> </td>
</tr>
<tr>
<td> <button onclick="str_palindrome ()" >Submit</button> </td>
</tr>
</table>
<div id="num"></div>
<div id="num1"> </div>
</body>
<script type="text/javascript">
function str_palindrome()
{
var len, i, reverse = '';
var string = document.getElementById ('first').value;
len = string.length;
for(i = len-1; i>=0; i--)
{
reverse = reverse+(string.charAt(i));
}
document.getElementById ('num').innerHTML ="Reverse of the string : "+ reverse;
if(string==reverse)
{
document.getElementById ('num1').innerHTML ="Given string is a palindrome";
}
else
{
document.getElementById('num1').innerHTML ="Given string is not a palindrome";
}
}
</script>
</html>