You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AI Playground测验机器人如何实现逐题弹出而非全部同时发送

问题原因

你代码最后多余的for循环是导致所有题目同时发送的核心问题:当用户输入start触发交互时,这个循环会遍历全部11个索引,一次性把所有题目全部发送出去。除此之外你的代码还存在结束逻辑提前触发的问题,刚进入第二级答题就会弹出结束提示,不符合答题流程。

修改后的完整代码

Bot.send ("Hi my name is lexi, a quiz bot");
Bot.send ("There will be a quiz of 2 levels containing 5 question each level");
Bot.send ("Question of First level will be from Science, Second level from GK");
Bot.send ("Type start to play");

var i = 0;
var score = 0;

async function respond(inputText){
    var questionList=[
        "What is the largest planet in the solar system?",
        "What is the largest gland in the body?",
        "The concept of gravity was discovered by which famous physicist?",
        "Which planet is also known by the name of red planet?",
        "What is the process by which solids directly turn into gas?",
        "",
        "Which is the largest flower in the world?",
        "Which country announced the ‘Intermediary Guidelines and Digital Media Ethics Code’?",
        "Which bird lays the largest eggs in the world?",
        "Who was the first President of India?",
        "Who is the winner of the US Open 2019 Men's title?",
    ];
    var answerList=[
        "jupiter",
        "liver",
        "isaac newton",
        "mars",
        "sublimation",
        "",
        "rafflesia",
        "india",
        "ostrich",
        "rajendra prasad",
        "rafael nadal",
    ];

    inputText = inputText.toLowerCase();
    // 输入start触发第一题
    if(inputText === 'start'){
        Bot.send(questionList[i]); 
        return;
    }
    // 所有题做完后不再响应答题逻辑
    if(i >= 11) {
        Bot.send("本次答题已结束,重新输入start可再次参与");
        return;
    }
    // 判断答案对错
    if (inputText == answerList[i]){
        Bot.send ("your answer is correct");
        score++;
     } else {
        Bot.send ("incorrect answer");
        Bot.send ("the right answer is "+ answerList[i]);
    }
    i = i+1;
    // 第一级结束判断
    if (i === 5){
        Bot.send ("First level is finished, Welcome to the second level of GK questions");
        i = i + 1;
    }
    // 所有题结束判断
    if(i >= 11) {
        Bot.send ("How was the quiz, If u were able to answer all questions very good, otherwise if u are able to not answer all try next time  ");
        Bot.send ("Your score is" + score );
        return;
    }
    // 发送下一题
    Bot.send(questionList[i]);
}

主要修改点

  • 删掉了末尾多余的for循环,避免一次性批量发送所有题目
  • 新增答题结束判定逻辑,避免所有题目做完后继续响应无效输入
  • 调整了结束提示的触发时机,改为10道题全部作答完成后再输出得分和结束语,不会在答题中途弹出
  • 把i递增的逻辑统一提取,避免重复代码降低出错概率

内容的提问来源于stack exchange,提问作者Jitendra Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 02:30:05