Results 1 to 10 of 10

Thread: Array Search

  1. #1
    Join Date
    Feb 2011
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Array Search

    I have the following array in a chatbot

    Code:
    Sites=new Array("alcatraz,California in the USA","eiffel tower,Paris in France","empire state building,New York in the USA","grand canyon,Arizona in the USA","leaning tower of pisa,Pisa in Italy");
    This piece of code searches the first part of the array and triggers the second part as a response. That all works fine. However, could it be rewritten in a way to eliminate the bailout response of "I don't know. Perhaps you can look it up on a map." to where it just skips over it if no match is found.

    I have more If statements further down the script that I want to use instead of the one bailout response. If nothing matches I have one big catchall to respond back with at the very end.


    Code:
    if (input.search("where is")!= -1)    
    {document.result.result.value = "I don't know. Perhaps you can look it up on a map.";
    for (i=0; i<Sites.length; i++) {
    Site=Sites[i].split(',');
    if (input.search(Site[0]) != -1) {
    document.result.result.value = Site[1];}
    }
    return true;}

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Have you tried on simply removing this part:
    Code:
    document.result.result.value = "I don't know. Perhaps you can look it up on a map.";
    ?
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. #3
    Join Date
    Feb 2011
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    yes, when I tried that it doesn't work. Nothing changes. The last response the bot used remains in the text box.

  4. #4
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Please copy-paste the entire code here as well as the HTML.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  5. #5
    Join Date
    Feb 2011
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    Here is a demo I am testing it on with the txt file attached. Thanks!

    http://www.frontiernet.net/~wcowart/aademo.html

  6. #6
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    What do you wanted to get done again? I tried to place something on the textbox like "eiffel tower" and the desired result doesn't seem to work though or I might be missing something.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  7. #7
    Join Date
    Feb 2011
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    Yes, it's these two pieces of code.

    So it would be like "where is the eiffel tower"

    I don't want the bail out response of "I don't know. Perhaps you can look it up on a map" to trigger. If "where is so and so" isn't in the array I want it to just skip it.


    Code:
    Sites=new Array("alcatraz,California in the USA","eiffel tower,Paris in France","empire state building,New York in the USA","grand canyon,Arizona in the USA","leaning tower of pisa,Pisa in Italy");

    Code:
    if (input.search("where is")!= -1)    
    {document.result.result.value = "I don't know. Perhaps you can look it up on a map.";
    for (i=0; i<Sites.length; i++) {
    Site=Sites[i].split(',');
    if (input.search(Site[0]) != -1) {
    document.result.result.value = Site[1];}
    }
    return true;}

  8. #8
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Did you tried removing the highlighted:
    Code:
    if (input.search("where is")!= -1)
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  9. #9
    Join Date
    Feb 2011
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    The "where is" part is necessary. To provide a response if someone types in

    Where is Alcatraz
    Where is eiffel tower
    Where is empire state building

    If someone types something not in the array such as

    Where is Yankee Stadium

    I want it to skip it and not trigger the bailout response.

  10. #10
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Then change this part:
    Code:
    if (input.search("where is")!= -1)
    ...to:
    Code:
    if (input.toLowerCase().substring(0,8)=='where is')

    ...and in order to remove the bailout response, comment them out:
    Code:
    // document.result.result.value = "I don't know. Perhaps you can look it up on a map.";
    .
    .
    .
    /* if (!flagrandom) {
    randomnum = [Math.floor(Math.random()*3)]
    flagrandom=true;} */

    Hope that helps.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

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
  •