PDA

View Full Version : Array Search



chriscc17
04-22-2011, 06:48 PM
I have the following array in a chatbot


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.



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;}

rangana
04-23-2011, 12:25 AM
Have you tried on simply removing this part:


document.result.result.value = "I don't know. Perhaps you can look it up on a map.";

?

chriscc17
04-23-2011, 12:53 AM
yes, when I tried that it doesn't work. Nothing changes. The last response the bot used remains in the text box.

rangana
04-23-2011, 12:59 AM
Please copy-paste the entire code here as well as the HTML.

chriscc17
04-23-2011, 01:31 AM
Here is a demo I am testing it on with the txt file attached. Thanks!

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

rangana
04-23-2011, 01:42 AM
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.

chriscc17
04-23-2011, 01:49 AM
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.



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");



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;}

rangana
04-23-2011, 02:09 AM
Did you tried removing the highlighted:


if (input.search("where is")!= -1)

chriscc17
04-23-2011, 02:20 AM
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.

rangana
04-23-2011, 03:30 AM
Then change this part:


if (input.search("where is")!= -1)


...to:


if (input.toLowerCase().substring(0,8)=='where is')



...and in order to remove the bailout response, comment them out:


// 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.