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

嵌套对象测验题选项随机生成问题求助及代码排查

解决测验题选项随机排序的问题

我来帮你分析代码无效的原因,再给出可行的解决方案。

为什么你的代码没起到随机排序的作用?

你尝试的这行代码 var index = Math.floor(Math.random() *currentQuestion.answers[letter]); 完全无法实现打乱效果,核心问题有两个:

  • currentQuestion.answers[letter] 是选项的具体文本值(比如"MJ"、"Pippen"),JavaScript 会尝试把字符串转为数字参与计算,非数字字符串会变成 NaN,最终 Math.floor(NaN) 还是 NaN,这行代码相当于无效代码
  • 你依然在按 for...in 循环的原始顺序往 answers 数组里添加选项,根本没有改变选项的排列顺序

正确的实现方案

要实现选项随机排序,我们需要先把嵌套的 answers 对象转换为可操作的数组,再用Fisher-Yates 洗牌算法(公平且高效的打乱方式)打乱数组,最后遍历打乱后的数组生成选项标签。

修正后的完整 createQuiz 函数如下:

function createQuiz() {
  // 清空问题容器
  document.getElementById('questionsBox').innerHTML = "";
  // 清空答案容器
  document.getElementById('answersBox').innerHTML = "";
  // 初始化答案存储数组
  answersCaptcha = [];
  // 初始化输出内容数组
  output = [];

  // 遍历每一道题
  myQuestions.forEach( (currentQuestion, questionNumber) => {
    const answers = [];
    const optionLabels = ["A","B","C"];

    // 1. 将answers对象转换为[key, value]格式的数组,方便后续打乱
    const answerPairs = Object.entries(currentQuestion.answers);
    
    // 2. 使用Fisher-Yates洗牌算法打乱数组
    for (let i = answerPairs.length - 1; i > 0; i--) {
      const randomIndex = Math.floor(Math.random() * (i + 1));
      // 交换当前元素和随机选中的元素位置
      [answerPairs[i], answerPairs[randomIndex]] = [answerPairs[randomIndex], answerPairs[i]];
    }

    // 3. 遍历打乱后的数组,生成选项标签
    answerPairs.forEach( ([key, value], index) => {
      answers.push(
        `<label>
           <input type="radio" name="question${questionNumber}" value="${key}">
           ${optionLabels[index]} : ${value}
         </label>`
      );
    });

    // 将当前题目和选项加入输出内容
    output.push(
      `<div class="question"> ${currentQuestion.question} </div>
       <div class="answers"> ${answers.join('')} </div>`
    );
  });

  document.getElementById('answersBox').innerHTML = output.join('');
  console.log(answersCaptcha);
}

关键逻辑解释

  • Object.entries(currentQuestion.answers):把嵌套的 answers 对象转成数组,每个元素是包含键(item1/item2)和值(选项文本)的数组,这样我们才能对选项进行排序操作
  • Fisher-Yates 洗牌算法:通过从后往前遍历数组,将当前元素与随机选中的前面元素交换位置,确保每个元素有均等的概率出现在任意位置,避免了简单随机排序可能出现的概率偏斜
  • 遍历打乱后的数组时,用 index 对应 optionLabels 里的 A/B/C,保证每个打乱后的选项都能正确对应字母标识

补充说明

你的 correctAnswer 存储的是选项的文本值(比如"MJ"),而单选框的 value 是对应的键(item1),所以后续判断答案是否正确时,需要通过选中的 value 去 currentQuestion.answers 里拿到对应的值,再和 correctAnswer 比较——这部分逻辑不受打乱顺序的影响,可以正常工作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:28:35