www.webdeveloper.com
Results 1 to 4 of 4

Thread: Variables with indexOf

  1. #1
    Join Date
    Oct 2007
    Posts
    38

    Variables with indexOf

    Hello there

    I'm trying to build a simple little bot that you can ask questions. I'm using the indexOf method but I'm not sure if this is the best option for what I'm trying to do. If you type 'hello' or 'how are you' it answers. Any other input results in 'Sorry but I didn't catch that. Say again?'. Is there a way that I can search input for certain words or combinations of words and trigger a single answer using indexOf or is there another method? Any help appreciated.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    
    <title>Bot</title>
    
    <script type="text/javascript">
    
    function bot(go){var a = go.getElementsByTagName('input');
    var q = a[0];var t = q.value.toLowerCase();document.go.reset();
    if(t.indexOf('hello')!=-1){document.getElementById('bot').innerHTML = "Hello to you.";return false;}
    if(t.indexOf('how are you')!=-1){document.getElementById('bot').innerHTML = "I am fine, thank you.";return false;};
    go.action=document.getElementById('bot').innerHTML = "Sorry but I didn't catch that. Say again?"; return false;};
    
    </SCRIPT>
    
    </head>
    
    <body onLoad="document.go.q.focus()">
    
    <div align="center">
    
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    
    <form name="go" onsubmit='return bot(go)' id="go" action=""><input name='q' type='text' /><input name='' type='hidden' /></form>
    
    <p>&nbsp;</p><div id="bot" align="center"></div>
    
    </div>
    
    </body>
    </html>

  2. #2
    Join Date
    Nov 2010
    Posts
    1,082
    there was a lot in your code that I didn't really understand why you had it there, so I took it out. You could simplify this further, but I suspect you are trying to do something else, so I guess this is as good a starting point as any.

    there was no way to fire your bot function, so I added an onblur to your text box. have a look and see if it's close tyo what you were thinking of

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    
    <title>Bot</title>
    
    <script type="text/javascript">
    
    function bot(){
    var a = document.getElementById('quest');
    if(a.value=='hello'){
    document.getElementById('bot').innerHTML = "Hello to you.";
    return false;}
    if(a.value=='how are you'){
    document.getElementById('bot').innerHTML = "I am fine, thank you.";
    return false;}
    else {
    document.getElementById('bot').innerHTML = "Sorry but I didn't catch that. Say again?"; 
    return false;}
    }
    </SCRIPT>
    
    </head>
    
    <body onLoad="document.go.q.focus()">
    
    <div align="center">
    
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    
    <form name="go" onsubmit='bot()' id="go" action=""><input name='q' id="quest" type='text' onblur='bot()'/><input name='' type='hidden' /></form>
    
    <p>&nbsp;</p><div id="bot" align="center"></div>
    
    </div>
    
    </body>
    </html>

  3. #3
    Join Date
    Oct 2007
    Posts
    38
    Thank you for replying. What I'm trying to do is prompt an answer from a list of keywords and ignore everything else. For example in my script the user can input 'Well hello there' and the bot responds 'Hello to you'. That doesn't work in your script. I'd like it so that if a user inputs 'well hello there', 'well howdy', or 'Greetings friend' it prompts 'Hello to you'. 'hello', 'howdy' and 'greetings' being the keywords in this example. Is this possible?

  4. #4
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Then you need to use the dictionary object and code to match expressions or part expressions. Your in the realm of RegExp(), match(), etc.
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

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