Jump to content




Recent Status Updates

  • Photo
      30 Sep
    rhossis

    laptop hard disk seated beneath motherboard but with no access panel. 7 hours to replace :(

    Show comments (3)
  • Photo
      19 Sep
    Chall

    I love it when you go to write a help thread, then while writing, you reach an enlightenment, and figure it out yourself.

    Show comments (3)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

Input Palindrome JavaScript

JavaScript

  • Please log in to reply
6 replies to this topic

#1 big-tony

big-tony

    CC Regular

  • Just Joined
  • PipPipPip
  • 33 posts

Posted 30 November 2009 - 11:51 AM

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:
<!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>


  • 0

#2 big-tony

big-tony

    CC Regular

  • Just Joined
  • PipPipPip
  • 33 posts

Posted 02 December 2009 - 09:47 PM

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:

<!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>

  • 0

#3 WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderator
  • 17,132 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 03 December 2009 - 09:30 AM

Basically, you need to check two strings:
1) the text with all whitespace/punctuation removed.
2) the text.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#4 DarkLordoftheMonkeys

DarkLordoftheMonkeys

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 232 posts

Posted 05 December 2009 - 11:52 AM

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.

Edited by DarkLordoftheMonkeys, 05 December 2009 - 12:48 PM.

  • 0
Life's too short to be cool. Be a nerd.

#5 DarkLordoftheMonkeys

DarkLordoftheMonkeys

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 232 posts

Posted 05 December 2009 - 12:34 PM

Okay, I just made a script and tried it out in Firefox, and it works:

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.");
	}
}

  • 0
Life's too short to be cool. Be a nerd.

#6 DarkLordoftheMonkeys

DarkLordoftheMonkeys

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 232 posts

Posted 05 December 2009 - 03:28 PM

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.
  • 0
Life's too short to be cool. Be a nerd.

#7 Lillian

Lillian

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 21 January 2010 - 03:14 AM

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