gpt4 book ai didi

javascript - 刚刚注意到我的 JavaScript 测验中的一个缺陷。我需要帮助来修复它

转载 作者:行者123 更新时间:2023-12-03 05:55:43 25 4
gpt4 key购买 nike

我最近在 JavaScript 测验中偶然发现了一个错误。这是一个多项选择测验。我点击了其中一个答案。然后我不断点击提交问题,直到测验完成。与其说我答对了 15 题中的 1 题,不如说我答对了 15 题中的 6 题,尽管我将其他问题留空。仅当我在选择选项后继续单击“提交”时,才会出现此计算错误。如有任何帮助,我们将不胜感激。

var pos = 0, test, test_status, question, choice, choices, chA, chB, chC, correct = 0;
var questions = [
["What does HTML stand for?", "High Tech Markup Language", "Hyper Text Multiple Listing", "Hyper Text Markup Language", "C"],
["What does CSS stand for?", "Computer Software Supervisor", "Cascading Stylesheets", "Computer Software Systems", "B"],
["What aspect of a website does JavaScript control?", "the behavior", "the structure", "the design & layout", "A"],
["What are media queries for?","They give the programmer access to covert media files.", "They make websites function well and look great on multiple devices like tablets and smartphones.", "They're a set of javascript libraries...like jquery.", "B"],
["The two categories of elements in html are block and...", "outline", "flatline", "inline", "C"],
["Which data type gives a value of true or false?", "character", "boolean", "integer", "B"],
["How do you write a comment in CSS?", "/* */", "//", "just write it out..", "A"],
["Java was developed by which company?", "Netscape", "Sun Microsystems", "Enron", "B"],
["Which gets more priority in CSS?", "class attribute", "element", "id attribute" ,"C"],
["PHP is a _________ language.", "server-side", "client-side", "westside", "A"],
["What does API stand for?", "Application Program Interface", "Apple Programs Iphones", "Advanced Programming Institute", "A"],
["Wordpress is a ...", "Content Management Device", "Content Manipulating Stylesheet", "Content Management System", "C"],
["Which of these is NOT a real programming language?", "C", "CSS", "JavaScript", "B"],
["In an HTML document it's best to store javascript in the ____ of the page.", "head", "bottom of the body", "top of the body", "B"],
["Bootstrap was built at which popular social media site?", "Twitter", "Facebook", "Instagram", "A"]
];
function _(x) {
return document.getElementById(x);
}
function renderQuestion() {
test = _("test");
if(pos >= questions.length){
test.innerHTML = "<h2>You got "+correct+" of "+questions.length+" questions correct</h2>";
_("test_status").innerHTML = "Test Completed";
pos = 0;
correct = 0;
return false;
}
_("test_status").innerHTML = "Question "+(pos+1)+" of "+questions.length;
question = questions[pos][0];
chA = questions[pos][1];
chB = questions[pos][2];
chC = questions[pos][3];
test.innerHTML = "<h3>"+question+"</h3>";
test.innerHTML += "<input type='radio' name='choices' value='A'> "+chA+"<br>";
test.innerHTML += "<input type='radio' name='choices' value='B'> "+chB+"<br>";
test.innerHTML += "<input type='radio' name='choices' value='C'> "+chC+"<br><br>";
test.innerHTML += "<button onclick='checkAnswer()'>Submit Answer</button>";
}
function checkAnswer(){
choices = document.getElementsByName("choices");
for(var i=0; i<choices.length; i++){
if(choices[i].checked){
choice = choices[i].value;
}
}
if(choice == questions[pos][4]){
correct++;
}
pos++;
renderQuestion();
}
window.addEventListener("load", renderQuestion, false);
body {
background: #f5f5f5;
}
* {
margin: 0;
padding: 0;
}
header {
width: 1000px;
height: 190px;
background-color: firebrick;
margin: 0 auto;
border-left: black solid 1px;
border-right: black solid 1px;

}
h1 {
color: white;
text-align: center;
position: relative;
top: 65px;
font-size: 3em;
font-family: cooper;
}
h2 {
position: relative;
top: 30px;
margin: 10px;
padding: 10px 40px 40px 40px;
}
h3 {
text-align: center;
font-size: 2em;
}
h5 {
color: white;
text-align: center;
font-size: 1.3em;
position: relative;
top: 110px;
}
p {
text-align: center;
font-size: 1.5em;
}
a {
text-decoration: none;
}
section {
width: 1000px;
height: 700px;
margin: 0 auto;
background-color: white;
border-left: black solid 1px;
border-right: black solid 1px;
}
#center {
text-align: center;
position: relative;
top: 20px;
}
#test {
color: black;
border: #000 1px solid;
padding: 10px 40px 40px 40px;
background-color: white;
margin-left: 20px;
margin-right: 20px;
position: relative;
top: 100px;
}
footer {
width: 1000px;
height: 250px;
background-color: #003366;
margin: 0 auto;
border-left: black solid 1px;
border-right: black solid 1px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Web Developer Quiz</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div="it">
<h2 id="test_status"></h2>
<div id="test"></div>
<button><a href="index.html"></a></button>
</div>
<script src="script.js"></script>
</body>
</html>

最佳答案

renderQuestion() 函数的开头,您应该重置 choice 变量

function renderQuestion() {
choice = undefined;
test = _("test");
...
}

关于javascript - 刚刚注意到我的 JavaScript 测验中的一个缺陷。我需要帮助来修复它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39938262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com