www.webdeveloper.com
Results 1 to 3 of 3

Thread: compare two strings

  1. #1
    Join Date
    Mar 2010
    Posts
    6

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

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,441

    Lightbulb Consider this ...

    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.

    Code:
    <html>
    <head>
    <title>Palindrome Check</title>
    </head>
    <body>
    <script type="text/javascript">
    // 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>=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");
    
    </script> 
    </body>
    </html>
    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.

  3. #3
    Join Date
    Feb 2010
    Location
    Amsterdam
    Posts
    174
    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/sh...d.php?t=226121
    /edit
    Last edited by Gozzy82; 03-11-2010 at 09:21 AM.

Thread Information

Users Browsing this Thread

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

Posting Permissions

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



Recent Articles