当前位置:首页 > JavaScript

js实现答题

2026-03-14 14:51:49JavaScript

使用 JavaScript 实现答题功能

基本答题逻辑

创建题目数组,包含题目、选项和正确答案:

const questions = [
  {
    question: "JavaScript 是一种什么语言?",
    options: ["标记语言", "脚本语言", "编译语言", "样式语言"],
    answer: 1
  },
  {
    question: "以下哪个不是 JavaScript 的数据类型?",
    options: ["String", "Boolean", "Number", "Float"],
    answer: 3
  }
];

显示题目功能

实现题目显示和选项渲染:

let currentQuestion = 0;
let score = 0;

function showQuestion() {
  const question = questions[currentQuestion];
  document.getElementById('question').textContent = question.question;

  const optionsContainer = document.getElementById('options');
  optionsContainer.innerHTML = '';

  question.options.forEach((option, index) => {
    const button = document.createElement('button');
    button.textContent = option;
    button.onclick = () => checkAnswer(index);
    optionsContainer.appendChild(button);
  });
}

检查答案功能

验证用户选择并更新分数:

function checkAnswer(selectedIndex) {
  const question = questions[currentQuestion];
  if (selectedIndex === question.answer) {
    score++;
    alert('回答正确!');
  } else {
    alert(`回答错误!正确答案是:${question.options[question.answer]}`);
  }

  currentQuestion++;
  if (currentQuestion < questions.length) {
    showQuestion();
  } else {
    showResult();
  }
}

显示结果功能

展示最终得分和答题统计:

js实现答题

function showResult() {
  const quizContainer = document.getElementById('quiz-container');
  quizContainer.innerHTML = `
    <h2>答题完成!</h2>
    <p>你的得分是:${score}/${questions.length}</p>
    <p>正确率:${(score/questions.length*100).toFixed(2)}%</p>
    <button onclick="location.reload()">重新答题</button>
  `;
}

HTML 结构示例

配套的基础 HTML 结构:

<div id="quiz-container">
  <h2 id="question"></h2>
  <div id="options"></div>
  <div id="result"></div>
</div>

进阶功能实现

添加计时器和题目类型判断:

js实现答题

let timeLeft = 30;
const timer = setInterval(() => {
  timeLeft--;
  document.getElementById('timer').textContent = `剩余时间:${timeLeft}秒`;
  if (timeLeft <= 0) {
    clearInterval(timer);
    checkAnswer(-1); // 超时自动跳过
  }
}, 1000);

随机题目功能

实现题目随机排序:

function shuffleQuestions() {
  for (let i = questions.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [questions[i], questions[j]] = [questions[j], questions[i]];
  }
}

本地存储功能

使用 localStorage 保存最高分:

function saveHighScore() {
  const highScore = localStorage.getItem('highScore') || 0;
  if (score > highScore) {
    localStorage.setItem('highScore', score);
  }
}

初始化函数

启动答题系统:

function initQuiz() {
  shuffleQuestions();
  showQuestion();
  document.getElementById('timer').textContent = `剩余时间:${timeLeft}秒`;
}

这些代码片段组合起来可以构建一个完整的 JavaScript 答题系统,包含题目显示、答案验证、计时功能和成绩统计等核心功能。

标签: js
分享给朋友:

相关文章

js实现轮播

js实现轮播

实现轮播图的基本思路 轮播图的核心逻辑是通过定时切换展示的图片或内容,通常结合左右滑动按钮和指示器(小圆点)增强交互性。以下是基于原生JavaScript的实现方法。 HTML结构 构建轮播图的HT…

js实现复制功能

js实现复制功能

使用 document.execCommand 方法 这种方法适用于较旧的浏览器,但在现代浏览器中可能被逐步淘汰。通过创建一个临时的 textarea 元素,将文本内容放入其中,然后执行复制命令。…

js实现定位

js实现定位

使用Geolocation API获取当前位置 在JavaScript中,可以通过浏览器内置的Geolocation API获取用户的地理位置信息。该API需要用户授权才能访问位置数据。 if (n…

js实现导航菜单

js实现导航菜单

使用HTML和CSS创建基础结构 导航菜单的基础结构通常由HTML的无序列表(<ul>)和列表项(<li>)组成,每个列表项包含一个链接(<a>)。CSS用于样式化…

js实现左右滑动

js实现左右滑动

实现左右滑动的 JavaScript 方法 监听触摸事件 通过 touchstart、touchmove 和 touchend 事件来检测用户的手势操作。记录触摸的起始位置和移动距离,判断滑动方向。…

js实现乘法

js实现乘法

实现乘法运算的方法 在JavaScript中实现乘法运算可以通过多种方式完成,以下列举几种常见方法: 基础运算符 直接使用乘法运算符*是最简单的方式: let result = 3 * 5; //…