代码之家  ›  专栏  ›  技术社区  ›  Barrie Reader

创建“假人工智能”聊天程序

  •  0
  • Barrie Reader  · 技术社区  · 15 年前

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

    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

    1 回复  |  直到 15 年前
        1
  •  2
  •   djdd87    15 年前

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

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

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

    Working version here.