用javascript写一个数据结构的考试卷
时间: 2023-08-04 12:30:49 浏览: 56
好的,以下是一个简单的数据结构考试卷示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>数据结构考试卷</title>
</head>
<body>
<h1>数据结构考试卷</h1>
<form>
<!-- 第1题 -->
<p><strong>1. 以下哪种数据结构可以通过链表实现?</strong></p>
<input type="radio" name="q1" value="a">栈<br>
<input type="radio" name="q1" value="b">队列<br>
<input type="radio" name="q1" value="c">散列表<br>
<input type="radio" name="q1" value="d">树<br><br>
<!-- 第2题 -->
<p><strong>2. 以下哪个算法可以用于对数组进行排序?</strong></p>
<input type="checkbox" name="q2" value="a">冒泡排序<br>
<input type="checkbox" name="q2" value="b">插入排序<br>
<input type="checkbox" name="q2" value="c">选择排序<br>
<input type="checkbox" name="q2" value="d">所有上述算法<br><br>
<!-- 第3题 -->
<p><strong>3. 以下哪种数据结构可以用于实现图?</strong></p>
<select name="q3">
<option value="a">链表</option>
<option value="b">栈</option>
<option value="c">队列</option>
<option value="d">邻接矩阵</option>
</select><br><br>
<!-- 第4题 -->
<p><strong>4. 在二叉树中,如果一个节点没有子节点,则称其为什么?</strong></p>
<input type="text" name="q4"><br><br>
<!-- 第5题 -->
<p><strong>5. 以下哪个算法可以用于查找数组中的元素?</strong></p>
<select name="q5">
<option value="a">冒泡排序</option>
<option value="b">插入排序</option>
<option value="c">选择排序</option>
<option value="d">二分查找</option>
</select><br><br>
<input type="submit" value="提交">
</form>
<script>
// 定义正确答案
const answers = {
q1: 'a',
q2: ['a', 'b', 'c', 'd'],
q3: 'd',
q4: '叶子节点',
q5: 'd'
};
// 获取表单元素
const form = document.querySelector('form');
const submitBtn = form.querySelector('input[type="submit"]');
// 表单提交事件
form.addEventListener('submit', function(event) {
event.preventDefault(); // 阻止表单默认提交
let score = 0; // 初始化得分
// 获取每个问题的答案
const q1 = form.querySelector('input[name="q1"]:checked').value;
const q2 = Array.from(form.querySelectorAll('input[name="q2"]:checked')).map(item => item.value);
const q3 = form.querySelector('select[name="q3"]').value;
const q4 = form.querySelector('input[name="q4"]').value;
const q5 = form.querySelector('select[name="q5"]').value;
// 比较答案并计算得分
if (q1 === answers.q1) {
score += 20;
}
if (JSON.stringify(q2) === JSON.stringify(answers.q2)) {
score += 20;
}
if (q3 === answers.q3) {
score += 20;
}
if (q4.trim() === answers.q4) {
score += 20;
}
if (q5 === answers.q5) {
score += 20;
}
// 显示得分
alert(`您的得分为:${score} 分`);
});
</script>
</body>
</html>
```
这是一个简单的 HTML 表单,包括了五道题目,每道题目有不同的类型,如单选、多选、填空和下拉选择,而每个选项都有对应的值。使用 JavaScript 来验证每个问题的答案,并在提交表单时计算分数。