gpt4 book ai didi

javascript测验,应显示适当的消息并提交按钮问题

转载 作者:行者123 更新时间:2023-12-03 08:18:14 25 4
gpt4 key购买 nike

我的任务包括为测验编写 JavaScript 代码。每一段代码都很好,但主要问题是我应该在测验结束时显示另一条消息,说明如果我答对了所有问题。应显示一条祝贺消息。 (例如,恭喜您答对了 3 个问题中的 3 个问题。)此外,如果当前问题尚未得到解答,我们将无法进入下一页。应禁用提交按钮。我应该使用警报框吗??

var pos = 0, test, test_status, question, choice, choices, chA, chB, chC, correct = 0;
var questions = [
[ "What is 10 + 4?", "12", "14", "16", "B" ],
[ "What is 20 - 9?", "7", "13", "11", "C" ],
[ "What is 7 x 3?", "21", "24", "25", "A" ],
[ "What is 8 / 2?", "10", "2", "4", "C" ]
];
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);
<h2 id="test_status"></h2>
<div id="test"></div>

var pos = 0, test, test_status, question, choice, choices, chA, chB, chC, correct = 0;
var questions = [
[ "What is 10 + 4?", "12", "14", "16", "B" ],
[ "What is 20 - 9?", "7", "13", "11", "C" ],
[ "What is 7 x 3?", "21", "24", "25", "A" ],
[ "What is 8 / 2?", "10", "2", "4", "C" ]
];
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);
<h2 id="test_status"></h2>
<div id="test"></div>

最佳答案

只需更改此行:

test.innerHTML = "<h2>You got "+correct+" of "+questions.length+" questions correct</h2>";

对于这个:

test.innerHTML = (correct == questions.length ? "CONGRATULATIONS!!! " : "") + "<h2>You got "+correct+" of "+questions.length+" questions correct</h2>";

关于javascript测验,应显示适当的消息并提交按钮问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33842240/

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