A little info:
A Palindrome is a word or phrase when spelled backwards is exactly the same work. For example: bob
"bob" is an example of a PERFECT palindrome.
A Standard palindrome is a word or phrase similar to the perfect palindrome except with punctuation marks or spaces.
Example: Madam, I'm Adam"
What I need to do is have an input box where you enter the word or phrase and have 2 buttons below it. 1 for determining if the input is a perfect palindrome and another button to detect if it is a standard palindrome.
Both buttons when clicked need to pop up in an alert box if the input is or isn't the specified palindrome type.
Here's an example of what I got soo far: Palindrome Checker
and the code:
Code:<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><center> <head> <title>Palindrome Checker</title> <script type="text/javascript" language="JavaScript"> <!-- function reverseString(the_word){ // reverseString takes a string and returns the reverse string var rString = ""; var alen = the_word.length; for (var i = alen ; i > 0 ; i--){ rString += the_word.charAt(i-1) }; return rString ; } function palindrome(the_word){ // palindrome takes a string and returns true if and only if the string // is a palindrome return (the_word == reverseString(the_word)); } function doCodeForPalin(){ // This code is the code to be executed on clicking the ``do it'' button var aStr = document.palinForm.inputWord.value; document.palinForm.outputWord.value = reverseString(aStr); document.palinForm.isPalin.value = palindrome(aStr); return true; } //--> </script> </head> <body bgcolor="white"> <h1>Palindrome Checker</h1> <form name="palinForm"> <p>You have 2 options.<br/> Check if the Palidrome is a <strong>PERFECT</strong> Palindrom <br/> Check if the Palidrome is a <strong>STANDARD </strong> Palindrome.</p> <p>Enter your word here: <input type="text" name="inputWord"><br> <input type="button" name= "doIt" value="Is it Perfect?" onclick= "doCodeForPalin();"> <input type="text" name="isPalin" value= ""><br> <input type="button" name= "doIt" value="Is it Standard?" onclick= "doCodeForPalin2();"> <input type="text" name="isPalin2" value= ""><br> <br/> Your Input Reversed: <input type="text" name="outputWord"><br/> </p> </form> </body></center> </html>
_____________________________________
Currently enrolled in School for an Associates Degree in Computer Science
Ok, I have everything the way I want it now EXCEPT actual code to detect if it's a PERFECT Palindrome (if it detects non alphabetical characters return it is NOT a perfect palindrome.
Here's my code so far:
Code:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/> <title> Palindrome Checker</title></head> <body><center> <script type="text/javascript"> <!--HIDE FROM INCOMPATIBLE BROWSERS //checks if the text in "input" is palindromic function palinChecker() { var the_palindrome = document.palindrome.input.value; document.palindrome.palindrometext.value = reversethe_palindrome(the_palindrome); if (isPalindrome(the_palindrome)) { alert("It IS a Standard palindrome."); } else { alert("That is not even a palindrome!"); } } function palinChecker2() { var the_palindrome = document.palindrome.input.value; document.palindrome.palindrometext.value = reversethe_palindrome(the_palindrome); if (isPalindrome(the_palindrome)) { alert("It IS a Perfect palindrome."); } else { alert("That is not even a palindrome!"); } } //checks if the upper-case and punctuation removed reverse //of the input the_palindrome is equal to itself reversed. function isPalindrome (inputthe_palindrome) { var reversedthe_palindrome = reversethe_palindrome(inputthe_palindrome); //use the reverse of the reverse here because //this will perform the punctuation elimination on both the_palindromes return (reversedthe_palindrome==reversethe_palindrome(reversedthe_palindrome)); } //uppercases, reverses, and removes all non-alphabetic //chars from the input the_palindrome function reversethe_palindrome(the_palindromeToReverse) { the_palindromeToReverse = the_palindromeToReverse.toUpperCase(); var reversedthe_palindrome=""; var length= the_palindromeToReverse.length-1; var ch; for (var i=length; i>=0; i--) { ch = the_palindromeToReverse.charAt(i) if(ch >='A' && ch <='Z') { reversedthe_palindrome += ch; } } return reversedthe_palindrome; } //STOP HIDING FROM INCOMPATIBLE BROWSERS--> </script> <form name="palindrome"> <p>Enter Word/Phrase: <input type="text" name="input"><br/><br/> <input type="button" name="check" value="Is it Standard?" onclick="palinChecker();"><br> <input type="button" name="check" value="Is it Perfect?" onclick="palinChecker2();"></p><br/> Your Input Reversed: <input type="text" name="palindrometext" value=""></p><br/> </form></body> </body> </html>
_____________________________________
Currently enrolled in School for an Associates Degree in Computer Science
Basically, you need to check two strings:
1) the text with all whitespace/punctuation removed.
2) the text.
I don't feel like reading your code as I suck at deciphering other people's code, but I would first remove the punctuation from both strings using String.replace(/[^A-Za-z0-9]/g, "") (you will have to put the replacements in new string variables as strings in Javascript are immutable), then put the characters of one string into an array, use the Array.reverse() method, use Array.join() to convert the reversed array back to a string, and compare that to the second string for equality.
Last edited by DarkLordoftheMonkeys; 12-05-2009 at 12:48 PM.
Life's too short to be cool. Be a nerd.
Okay, I just made a script and tried it out in Firefox, and it works:
Code:function stringCompare(str1, str2){ var string1 = str1.value; var string2 = str2.value; var nopunc1 = string1.replace(/[^A-Za-z0-9]/g, ""); var nopunc2 = string2.replace(/[^A-Za-z0-9]/g, ""); var lower1 = nopunc1.toLowerCase(); var lower2 = nopunc2.toLowerCase(); var arr = new Array(); for( var i = 0; i < lower1.length; i++ ){ arr[i] = lower1.charAt(i); } arr = arr.reverse(); var lowerR = arr.join(""); if( lowerR == lower2 ){ alert("They are palendromes."); } else{ alert("They are not palendromes."); } }
Life's too short to be cool. Be a nerd.
Sorry, I misread your post. I thought you were trying to write a program that will tell if one sentence is the reverse of the other. To find if one sentence is a palindrome, you would have both inputs to the function be the same string.
Life's too short to be cool. Be a nerd.
hi! I don't know what is Palindrome Checker...?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks