【问题标题】:Creating a "fake AI" chat program创建一个“假 AI”聊天程序
【发布时间】:2011-04-12 14:42:33
【问题描述】:

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

我的以下功能无法正常工作 (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...

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

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

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

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

    Working version here.

    【讨论】:

    • @Neurofluxation - 实际上我已经在你的上一个问题中给了你一个例子:) stackoverflow.com/questions/3716477/…
    • 道歉 - 没有看到 - 我有另一个有效的答案 - 仍然为你 +2rep(不要抱怨!^_^)
    猜你喜欢
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 2011-07-10
    • 1970-01-01
    • 2012-04-04
    • 2017-03-05
    相关资源
    最近更新 更多