Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Javascript help

  1. #1
    Join Date
    Apr 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Javascript help

    Hello

    I need some help with Javascript and i m stuck with this simple thing that i m unable to figure out.

    i need some help with this....

    Suppose i m having two words in an array

    array = new Array();

    array[0] = "abel";

    array[1] = "able";

    i need to check if all the letters in the array at position 0 and position 1 are the same... like at pos. 0 the letters in alphabetical order are "a", "b", "e", "l" adn if i can sort the word at pos. 1 in the same order and match it using an IF condition, i will achieve the result.

    I want to get something like this

    if(array[0] == array[1]){
    alert("The word is an anagram");
    }

    any quick help with idea or code will be appreciated

    Thanx

  2. #2
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Well I barely am good at arrays but may be able to help you out, try this -

    <script>
    array = new Array()
    array[0] = "abel"
    array[1] = "able"
    if (array[0].substring(0,0)==array[1].substring(0,0)) {
    alert("The word is an anagram")}
    </script>

    Well it should work

  3. #3
    Join Date
    Apr 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanx buddy

  4. #4
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Anytime

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That only seems to check the first letter but, it's a neat idea to use substring. This will work for the whole word:

    Code:
    <script type="text/javascript">
    var words = [];
    words[0] = "Abel";
    words[1] = "able";
    function pad_sort(w){
    var t='';
    for (var i_tem = 0; i_tem < w.length; i_tem++)
    t+=w.charAt(i_tem).toLowerCase()+',';
    return t.split(',').sort().join();
    }
    function isanagram(w1,w2){
    if(pad_sort(w1)==pad_sort(w2))
    alert('anagram');
    }
    isanagram(words[0],words[1]);
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Or you could just do -

    <script>
    array = new Array()
    array[0] = "abel"
    array[1] = "able"
    if (array[0]==array[1]) {
    alert("The word is an anagram")}
    </script>

    An dif you want to include the else statement -

    <script>
    array = new Array()
    array[0] = "abel"
    array[1] = "able"
    if (array[0]==array[1]) {
    alert("The word is an anagram")}
    else {
    alert("The word is not an anagram")}
    </script>

    Though I am not sure what an anagram is....

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Do you even know what an anagram is?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Lol you must of cross posted

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That question was for both of the other two folks posting in this thread. Here's an answer to it:

    Main Entry: 1an·a·gram
    Pronunciation: 'a-n&-"gram
    Function: noun
    Etymology: probably from Middle French anagramme, from New Latin anagrammat-, anagramma, modification of Greek anagrammatismos, from anagrammatizein to transpose letters, from ana- + grammat-, gramma letter -- more at GRAM
    1 : a word or phrase made by transposing the letters of another word or phrase
    2 plural but singular in construction : a game in which words are formed by rearranging the letters of other words or by arranging letters taken (as from a stock of cards or blocks) at random
    I think #1 is what we should be going for here.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I don't think I will be using that word

Bookmarks

Posting Permissions

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