Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Chat Bot

Status
Not open for further replies.

chriscc17

Programmer
Jul 5, 2010
4
US
I have a pattern matching chatbot where I use the following to select at random the various responses.

Code:
if (input.search("favorite food")!= -1) {
message[0] = "Pizza!"; 
message[1] = "Sub sandwich."; 
message[2] = "Tacos."; 
message[3] = "Cheeseburger"; 
[COLOR=red]num = [Math.floor(Math.random()*4)][/color]
document.result.result.value = message[num]; 
return true;}

Is there an easy way to change the above to where it always selects message[0] first and then goes in numerical order. For example message[0] then message[1] then message[2] then message[3]. After message[3] is triggered then it starts back to message[0]

Thanks!
 
Loop?

Put your var num outside the function this is in, and them simply increase it each time:

Code:
if(num > 3){
num=0;
}
document.result.result.value = message[num];
num++;
return true;}



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
I guess I don't understand how to combine the two to get it to work.

I need to place this outside the below?

var num = 0;

Then something like this?

Code:
if (input.search("zfood")!= -1) {
message[0] = "Pizza!"; 
message[1] = "Sub sandwich."; 
message[2] = "Tacos."; 
message[3] = "Cheeseburger"; 
if(num > 3){
num=0;
}
document.result.result.value = message[num];
num++;
return true;}
 
Yup that's the gist of it.

Code:
var num=0;

....

function somefunction(){
...
message[3] = "Cheeseburger";
if(num > 3){
num=0;
}
document.result.result.value = message[num];
num++;
...
}

The only reason to move the variable outside the function is so it doesn't reset to 0 each time the function is called, and retains its value through multiple calls of the function.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
It's working in the sense if you type "food" 5 or 6 straight times it will start with message[0] and loop accordingly. However, if I type another response then go back to it the sequence is off. Here is a real small demo I'm testing it on. Not sure where my problem is.

 
The problem is that your other tests also affect the num variable.

That is when you type "i know" or something else it still goes and randomizes the num variable to produce a response from the options.

You could give each string search a different variable to randomize for the answers. numfood, numiknow, numother etc...





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Ok, that makes sense and it works. Thanks, I appreciate your time and your responses.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top