Click to See Complete Forum and Search --> : Palindrome
pokeyq
10-12-2003, 03:22 PM
I am new to JavaScript and having a problem with Palindromes.
I need to extract a palindrome from a string. It needs to show the reversal in a seperate field and also state in another field if it "is" or "is not" a palindrome.
I would be soooo happy if someone could please help me with this!!!
Charles
10-12-2003, 03:48 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>
<script type="text/javascript">
<!--
String.prototype.reverse = function () {return this.split('').reverse().join('')}
String.prototype.isPalindrome = function () {return this.toLowerCase().replace(/\W/g, '') == this.toLowerCase().replace(/\W/g, '').reverse()}
// -->
</script>
<form action="">
<div>
<label>String<input type="text" onchange="this.form.reverse.value = this.value.reverse(); this.form.palindrome.value = this.value.isPalindrome()"></label>
<label>Reverse<input type="text" name="reverse" readonly></label>
<label>Is Palindrome<input type="text" name="palindrome" readonly></label>
</div>
</form>
pokeyq
10-12-2003, 07:10 PM
This is a copy of the form I am working on that needs the palindrome, I hope this helps...
<html>
<body>
<script language="javascript">
function config()
{
//calculate length of word
var Word = sentence.value
var Length = Word.length
characters.value = Length
//calculate the number of words
var Words = 0
for (x=0; x <= Word.length - 1; x++)
{
if (Word.charAt(x)==" ")
Words++;
}
words.value = Words+1
//calculate the number of vowels
var Vowels = 0
for ( x=0; x<=Word.length -1; x++)
{
if (Word.charAt(x) == "a" || Word.charAt(x) == "e" || Word.charAt(x) == "i" || Word.charAt(x) == "o" || Word.charAt(x) == "u"
|| Word.charAt(x) == "A" || Word.charAt(x) == "E" || Word.charAt(x) == "I" || Word.charAt(x) == "O" || Word.charAt(x) == "U")
Vowels++;
}
vowels.value = Vowels
//calculate the number on consonants
var Cons = 0
for ( x=0; x<=Word.length -1; x++)
{
if (Word.charAt(x) == "b" || Word.charAt(x) == "c" || Word.charAt(x) == "d" || Word.charAt(x) == "f" || Word.charAt(x) == "g"
|| Word.charAt(x) == "h" || Word.charAt(x) == "j" || Word.charAt(x) == "k" || Word.charAt(x) == "l"
|| Word.charAt(x) == "m" || Word.charAt(x) == "n" || Word.charAt(x) == "p" || Word.charAt(x) == "q" || Word.charAt(x) == "r"
|| Word.charAt(x) == "s" || Word.charAt(x) == "t" || Word.charAt(x) == "v" || Word.charAt(x) == "w" || Word.charAt(x) == "x"
|| Word.charAt(x) == "y" || Word.charAt(x) == "z" || Word.charAt(x) == "B" || Word.charAt(x) == "C" || Word.charAt(x) == "D"
|| Word.charAt(x) == "F" || Word.charAt(x) == "G" || Word.charAt(x) == "H" || Word.charAt(x) == "J" || Word.charAt(x) == "K"
|| Word.charAt(x) == "L" || Word.charAt(x) == "M" || Word.charAt(x) == "N" || Word.charAt(x) == "P" || Word.charAt(x) == "Q"
|| Word.charAt(x) == "R" || Word.charAt(x) == "S" || Word.charAt(x) == "T" || Word.charAt(x) == "V" || Word.charAt(x) == "W"
|| Word.charAt(x) == "X" || Word.charAt(x) == "Y" || Word.charAt(x) == "Z")
Cons++;
}
consonants.value = Cons
//locate the substring
var Word = sentence.value
var search = sub.value
locate.value = Word.indexOf(search)
//convert to uppercase
uppercase.value = Word.toUpperCase()
//convert to lowercase
lowercase.value = Word.toLowerCase()
//reverse string
var Rev = ""
for (x = Word.length-1; x>=0; x--)
{
Rev = Rev + Word.charAt(x)
}
reversed.value = Rev
//Is the string a palindrome
}
</script>
Type your string here: <input type = "text" name = "sentence" size = "50"><p>
Type the substring you want to locate: <input type = "text" name = "sub" size = "10"><p>
<input type = "button" value = "submit" onclick = "config()"><p>
The number of characters: <input type = "text" name = "characters" size = "2"><p>
The number of words in your string: <input type = "text" name = "words" size = "2"><p>
The number of vowels in your string: <input type = "text" name = "vowels" size = "2"><p>
The number of consonants in your string: <input type = "text" name = "consonants" size = "2"><p>
Location where your substring is found: <input type = "text" name = "locate" size = "2"><p>
Your string in uppercase: <input type = "text" name = "uppercase" size = "50"><p>
Your string in lowercase: <input type = "text" name = "lowercase" size = "50"><p>
Your string reversed: <input type = "text" name = "reversed" size = "50"><p>
Your string <input type = "text" name = "returned" size = "3"> a palindrome.<p>
</body>
Charles
10-12-2003, 07:48 PM
Boy do you need to learn about Regular Expressions (http://devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/regexp.html).
pokeyq
10-12-2003, 08:14 PM
Thank you, I need to learn a lot. The instructor I have has never taught JavaScript. The college didn't bother telling anyone this. He is learning as he is teaching so you can only imagine how confusing this is for me. :(
pokeyq
10-12-2003, 10:13 PM
pokeyq
10-13-2003, 11:51 PM
Whew!!! I got it...
if (Word == Rev)
returned.value = "IS"
else
returned.value = "IS NOT"
Charles
10-14-2003, 04:41 AM
Originally posted by pokeyq
Whew!!! I got it...
if (Word == Rev)
returned.value = "IS"
else
returned.value = "IS NOT" Except that you have to filter out white space and comma's and such and you have to make it case insensitive. "Madam, I'm adam" is generally considered a palindrome.