Closed Thread
Results 1 to 7 of 7

Thread: Input Palindrome JavaScript

  1. #1
    big-tony is offline Learning Programmer
    Join Date
    Jan 2009
    Location
    Tennessee
    Posts
    42
    Rep Power
    0

    Input Palindrome JavaScript

    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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    big-tony is offline Learning Programmer
    Join Date
    Jan 2009
    Location
    Tennessee
    Posts
    42
    Rep Power
    0

    Re: Input Palindrome JavaScript

    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

  4. #3
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Input Palindrome JavaScript

    Basically, you need to check two strings:
    1) the text with all whitespace/punctuation removed.
    2) the text.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Re: Input Palindrome JavaScript

    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.

  6. #5
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Re: Input Palindrome JavaScript

    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.

  7. #6
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Re: Input Palindrome JavaScript

    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.

  8. #7
    Lillian is offline Newbie
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Re: Input Palindrome JavaScript

    hi! I don't know what is Palindrome Checker...?

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Javascript to Add a Text Input field
    By ofseo in forum JavaScript and CSS
    Replies: 27
    Last Post: 01-12-2012, 07:17 PM
  2. How to store the user input in the form of transcripts in Javascript?
    By swathisambaraj in forum JavaScript and CSS
    Replies: 6
    Last Post: 06-23-2010, 01:05 AM
  3. PALINDROME anyone...
    By mdeenny in forum C and C++
    Replies: 3
    Last Post: 10-29-2009, 09:09 AM
  4. Javascript to Add a Text Input field
    By lincolnHawk in forum JavaScript and CSS
    Replies: 6
    Last Post: 05-30-2009, 04:02 PM
  5. A palindrome
    By ahmed in forum C and C++
    Replies: 11
    Last Post: 11-29-2008, 06:51 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts