faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

2 of 4 people (50%) answered Yes
Recently 2 of 4 people (50%) answered Yes

Entry

How would I write a script to test whether a word is palindrome?

Apr 8th, 2008 18:40
ha mo, Dave Clark, chad calvert,


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Chad,
     Here's one way at least:
var str = "A man, a plan, a canal, Panama!";
//
var len, str = str.toLowerCase().replace(/[^a-z]/g, '');
var x, half = Math.ceil((len = str.length) / 2);
for (x=0; x<half; ++x)
{
    if (str.substr(x,1) != str.substr(len-1-x,1)) break;
}
if (x < half)
  alert('String is not a palindrome.');
else
  alert('String is a palindrome.');
Take care,
Dave Clark
www.DaveClarkConsulting.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com