大家好,我一直在创建一个小聊天机器人(为了娱乐和练习)。
我有以下功能无法正常工作( 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
...
最佳答案
$.inArray
不返回 true 或 false,它返回基于 0 的索引。
-1表示未找到,> -1是数组中匹配的索引:
if ($.inArray(i.val(), helloInputArray) > -1) {
// The item was in this array
}
关于javascript - 创建 "fake AI"聊天程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3716656/