创建“假AI”聊天程序

嘿大家,我一直在创建一个小聊天机器人(为了好玩和练习)。

我有以下function无法正常工作( 此处完整代码 ):

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...
"); i.val(''); i.focus(); } } }

似乎总是返回helloOutputArray

$.inArray不返回true或false,它返回一个基于0的索引。

-1表示未找到,> -1是数组中匹配的索引:

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

这里有工作版。