Question :- Write a javascript function to check whether a word or a sentence is palindrome or not irrespective of case and spaces. Name the HTML file as palin.html.
Give appropriate alerts on click of a button name is "palinbtn". Also provide a text box named "palin" which accepts the word / sentence.
Important Note :
- Remove all white spaces from the input given and check for palindrome of the same input ignoring case.
- After displaying the appropriate message through alert(), the page should not get redirected.
- Do not use 'let' or 'const' keywords. Instead, use 'var'.
- Use getElementById() or getElementsByName() to fetch value out of the HTML components.
- Make sure all tags and attributes are in lower case
Code:-
<!DOCTYPE html>
<html>
<body>
//input from user using form
<form onsubmit="return display();">
Enter word/sentence to check for palindrome:<input
type="text"
name="palin"
id="palin"
/><br />
<input type="submit" name="palinbtn" value="Check Palindrome" />
</form>
<script>
function display() {
//getting the value from textbox
var str = document.getElementById("palin").value;
//removing special char. and converting to lowercase
var str = str.replace(/\s/g, "").toLowerCase();
//removing whitespaces
var input = str.split();
//joining the reversed string
var output = input.reverse().join("");
if (str == output) {
alert("The entry is a Palindrome.");
return false;
} else {
alert("The entry is not a palindrome");
return false;
}
}
</script>
</body>
</html>