javascript - 创建 "fake AI"聊天程序

标签 javascript jquery

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

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

Working version here.

关于javascript - 创建 "fake AI"聊天程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3716656/

相关文章:

javascript - 允许您在 html 中粘贴小型 JavaScript 脚本的网站如何跟踪您的网站?

javascript - 根据下拉值自动填充文本框

javascript - 单击“选择选项”时,使下拉菜单消失

jquery - 更改滚动时的 div 不透明度

php - 将 php 中的日期时间值填充到 Jquery Moble 文本输入 datetime-local 中

javascript - Android WebView 无法找到 JavaScript 函数

Javascript 包装元素并在同一位置插入

javascript - 我可以在我的自定义 Polymer 元素中使用 jquery 吗?

javascript - 如何从 Html.ActionLink 转换为链接到 Ajax 调用的按钮?

jquery - 向上/向下滑动下拉菜单