在测验中显示正确和不正确
Showing Correct and Incorrect in a quiz
我有一个Javascript测试,运行良好,显示有多少正确答案。但是,我需要它也显示错误的问题及其答案。
var score = 0;
var questions = [
['Question One?', '1 Answer'],
['Question Two?', '2 Answer'],
['Question Three?', '3 Answer'],
['Question Four?', '4 Answer'],
['Question Five?', '5 Answer'],
['Question Six?', '6 Answer']
];
var cA = [];
function askQuestion(question) {
var answer = prompt(question[0], '');
if (answer == question[1]) {
alert('Correct!');
score++;
cA = question;
} else {
alert('Sorry. The correct answer is ' + question[1]);
}
}
for (var i = 0; i < questions.length; i++) {
askQuestion(questions[i]);
}
var message = 'You got ' + score;
message += ' out of ' + questions.length;
message += ' questions correct.';
document.write('<p>' + message + '</p>' + cA);
如何显示与答案不一致的问题?
试试这个;)
var score = 0;
var questions = [
['Question One?', '1 Answer'],
['Question Two?', '2 Answer'],
['Question Three?', '3 Answer'],
['Question Four?', '4 Answer'],
['Question Five?', '5 Answer'],
['Question Six?', '6 Answer']
];
var cA = [];
var incorrect = [];
function askQuestion(question, index) {
var answer = prompt(question[0], '');
if (answer == question[1]) {
alert('Correct!');
score++;
cA = question;
} else {
incorrect.push(index);
alert('Sorry. The correct answer is ' + question[1]);
}
}
for (var i = 0; i < questions.length; i++) {
askQuestion(questions[i], i);
}
var message = 'You got ' + score;
message += ' out of ' + questions.length;
message += ' questions correct.<br>' + cA;
message += '<br><br>You answered ' + incorrect.length;
message += ' questions incorrect. These are as follows:';
for(var i in incorrect){
message += '<p>Q ' + (incorrect[i] + 1) + '. ' + questions[incorrect[i]][0] + '<br>' + questions[incorrect[i]][1] + ' </p>';
}
document.write('<p>' + message + '</p>');
相关文章:
- 日历显示不正确
- 引导程序下拉列表显示不正确
- Highcharts热图显示不正确
- 为什么WebBrowser控件在保存后不能正确显示HTML
- 当使用javascript/jquery添加值时,html文本输入表显示不正确
- HTML 页面在通过 Chrome 扩展程序加载时显示不正确
- 文档的第一行.写的格式不正确,有时无法显示
- 格式为nnnnnnnn的字符串在电子表格中显示为日期或不正确
- CKEDITOR的内容显示不正确
- 在测验中显示正确和不正确
- 货币符号不't在javascript中正确显示
- 当非整小时偏移时,带有 Moment 的时区显示不正确.js
- 为什么输入值显示不正确
- D3.js线形图轴不正确显示数据
- 添加样式表到xml与javascript不正确显示或根本
- 选择更改后不正确显示
- jQuery append不正确显示浮点数
- 边界框不显示文本,填充框不正确显示
- 单词中的字母在IE7中正确显示,而在IE8中不正确显示
- HTML电子邮件文本不正确显示在Gmail移动