2010-09-15 122 views
0

嘿大家好,我一直在創建一個小聊天機器人(用於娛樂和練習)。創建一個「假AI」聊天程序

我有一個不能正常工作下面的函數(FULL CODE HERE):

function runAI() { 
      if (i.val().length > 0) { 
       if ($.inArray(i.val(), helloInputArray)) { 
        r = Math.floor(Math.random()*4);       
        o.html(o.html()+helloOutputArray[r]); 
        i.val(''); 
        i.focus(); 
       } else if ($.inArray(i.val(), byeInputArray)) { 
        r = Math.floor(Math.random()*4);       
        o.html(o.html()+byeOutputArray[r]); 
        i.val(''); 
        i.focus(); 
       } else { 
        o.html(o.html()+"I don't know what that means...<br />"); 
        i.val(''); 
        i.focus(); 
       } 
      } 
     } 

它似乎總是返回helloOutputArray ...

回答

2

$.inArray不返回true或false,則返回基於0的索引。

-1表示沒有找到,> -1是匹配數組中的索引:

if ($.inArray(i.val(), helloInputArray) > -1) { 
    // The item was in this array 
} 

Working version here.

+0

完美,非常感謝你 - 會很快接受。 – 2010-09-15 10:19:19

+1

@Neurofluxation - 我實際上已經給你在你最後一個問題的例子:) http://stackoverflow.com/questions/3716477/is-there-a-way-where-i-can-hold-all-potential - 數組/ 3716495#3716495 – GenericTypeTea 2010-09-15 10:20:21

+0

道歉 - 沒有看到 - 我有另一個有效的答案 - 仍然+ 2rep爲你(不要抱怨!^ _ ^) – 2010-09-15 10:21:36