首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建一个“假AI”聊天程序

创建一个“假AI”聊天程序
EN

Stack Overflow用户
提问于 2010-09-15 18:13:26
回答 1查看 1.2K关注 0票数 0

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

我有以下函数不能正常工作(FULL CODE HERE):

代码语言:javascript
复制
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...

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-09-15 18:17:25

$.inArray不返回true或false,它返回一个从0开始的索引。

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

代码语言:javascript
复制
if ($.inArray(i.val(), helloInputArray) > -1) {
    // The item was in this array
}

Working version here.

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3716656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档