/    Sign up×
Community /Pin to ProfileBookmark

compare two strings

How do I take two strings and compare them? My last attempt was to put them into arrays but that causes an error. I am stuck.

<script type=”text/javascript”>
/* <![CDATA[ */
//declare s1 as a variable and prompt user for a word
var s1 = prompt(“enter a word”);
s1 = s1.toUpperCase() // make word upper case
var j;
// reverses the word entered by the user
for (var i = s1.length – 1; i>=0; i–, j++)
{
var s2 = s1; // take word that was reversed and put it in s2
}
// turn them into an array
var arrays = new array();
var array1[] = s1;
var array2[] = s2;

// compare the arrays and display message
for( var j=0;j<array1.length;j++)
{
if(array1[j] == array2[j])
{
alert( s1 + ” is a palindrome”);
else
alert( s1 + ” is not a palindrome”);
}
}
// compare the words and display a message
//if (s1 == s2)
//alert( s1 + ” is a palindrome”);
//else
// alert( s1 + ” is not a palindrome”);
/* ]]> */
</script>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMar 11.2010 — No need to use arrays. Not the best way to do this, but for simple words it will work.

Note: No checks for punctuation or spaces if used to check for palindrome phrases.

<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Palindrome Check&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?t=226128

//declare s1 as a variable and prompt user for a word
var s1 = prompt("enter a word");

// reverses the word entered by the user
function reverseWord(s1) {
s1 = s1.toUpperCase() // make word upper case
var s2 = '';
for (var i = s1.length - 1; i&gt;=0; i--) {
s2 += s1[i]; // take word that was reversed and put it in s2
}
return s2;
}

/* INVALID ARRAY ASSIGNMENT
// turn them into an array
var arrays = new array();
var array1[] = s1;
var array2[] = s2;
*/

// compare the words and display message
if(s1.toUpperCase() == reverseWord(s1)) {
alert( s1 + " is a palindrome");
} else {
alert( s1 + " is not a palindrome");
}

// compare the words and display a message
//if (s1 == s2)
//alert( s1 + " is a palindrome");
//else
// alert( s1 + " is not a palindrome");

&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;


Note: remove extra comments from original post after you see why it was not needed.

BTW: Please use [ code] and [ /code] tags (without the spaces)

to make it easier to read you code in the forum.
Copy linkTweet thisAlerts:
@Gozzy82Mar 11.2010 — you can shorten this up a bit

function isAPalinDrom(txt){

txt = txt.toUpperCase(); // make uppercase like above function

return txt.split('').reverse().join('') === txt;

}


edit

extending the string object is a better solution:

http://www.webdeveloper.com/forum/showthread.php?t=226121

/edit
×

Success!

Help @xravensxdreamx spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...