A palindrome is a word or phrase that is identical forward or backward, such as
ID: 3538643 • Letter: A
Question
A palindrome is a word or phrase that is identical forward or backward, such as the word %u201Cracecar.%u201D A standard palindrome is similar to a perfect palindrome except that spaces and punctuation are ignored. For example, %u201CMadam, I%u2019m Adam%u201D is a standard palindrome because the characters are identical forward or backward, provided that you remove the spaces and punctuation marks. Write a script that checks whether a word or phrase entered by a user is a palindrome. Use a form where the user can enter the word or phrase, and include one button that checks if the word or phrase is a perfect palindrome and another button that checks if the word or phrase is a standard palindrome. Both buttons should display an alert dialog box that states whether the phrase is a perfect or standard palindrome. For both types of palindromes, you need to use the reverse() method of the Array class. For the standard palindrome, use a regular expression to determine whether each character is an alphanumeric character; if not, then you need to remove the nonalphanumeric character (or space) before you can determine if the word or phrase is a standard palindrome. Save the document as Palindromes.html.
I need to add whether a phrase is a standard palindrome.
This is what I have that is working so far:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Palindrome Checker</title>
<link rel="stylesheet" href="./Cases/js_styles.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
/* <![CDATA[ */
/* ]]> */
function Palindrome()
{
var revStr = "";
var str = document.getElementById("str").value;
var i = str.length;
for(var j=i; j>=0; j--)
{
revStr = revStr+str.charAt(j);
}
if(str == revStr)
{
alert(str+" -is a perfect Palindrome");
} else
{
alert(str+" -is not a perfect Palindrome");
}
}
</script>
<form >
Enter a word to check if it is a palindrome: <input type="text" id="str" name="string" /><br />
<input type="submit" value="Check"/>
</form>
</body>
</html>
Explanation / Answer
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Palindrome Checker</title>
<link rel="stylesheet" href="./Cases/js_styles.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
/* <![CDATA[ */
/* ]]> */
function PerfectPalindrome()
{
var str = document.getElementById("str").value;
var revStr = str.split("").reverse().join("");
if(str == revStr)
{
alert(str+" -is a perfect Palindrome");
} else
{
alert(str+" -is not a perfect Palindrome");
}
}
function StandardPalindrome()
{
var str1 = "";
var str2 = "";
var str = document.getElementById("str").value;
var str2=str.toLowerCase();
for(var i=0; i<str.length; i++)
{
var char1 = str2.charAt(i);
var cc = char1.charCodeAt(0);
if((cc>47 && cc<58) || (cc>96 && cc<123) )
{
str1 = str1 + str2.charAt(i);
}
}
var revStr = str1.split("").reverse().join("");
if(str1 == revStr)
{
alert(str+" -is a standard Palindrome");
} else
{
alert(str+" -is not a standard Palindrome");
}
}
</script>
<form >
Enter a word to check if it is a palindrome: <input type="text" id="str" name="string" /><br />
<input type="submit" value="Check Perfect"/>
<input type="submit" value="Check Standard"/>
</form>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.