【发布时间】:2015-03-25 20:13:24
【问题描述】:
基本上我有我的程序,这是一个测验,我的一个布尔值得到了意想不到的结果。我所指的布尔条件是 checkCorrectAnswer 函数中的条件。在这里,我正在测试是否点击了正确的答案——如果有,那么它应该评估为真,否则为假,并在 else 语句之后执行所有操作。然而,if 条件总是被评估为假,即使我点击正确的答案,我也会得到“不正确”。
以下是程序代码:
window.onload = function() {
var attr;
var currentQuestion = 0;
var allQuestions = [{
question: 'Which turkish club did former Leeds player Harry Kewell join in 2008, which caused an uproar amongst Leeds supporters?',
choices: ['Galatasaray', 'Besiktas', 'Fenerbahce', 'Sivaspor'],
correctAnswer: 0
},
{
question: 'Who is the former Liverpool star who beat Ruud Van Nistelrooy\'s record of most prolific foreign goalscorer in their debut in the Premier League?',
choices: ['Micheal Owen', 'Xabi Alsonso', 'Luis Suarez', 'fernando Torres'],
correctAnswer: 3
},
{
question: 'Who scored Liverpool\s winner in \'that\' first 4-3 game against Kevin Keegan\'s Newcastle United in April 1996?',
choices: ['Stan Collymore', 'Phil Baab', 'Steven Gerrard', 'Jamie Carragher'],
correctAnswer: 0
},
{
question: 'Which former Aston Villa and Ireland midfielder went on to become a regular TV pundit with ITV?',
choices: ['Dwight Yorke', 'Stan Collymore', 'Andy Townsend', 'Steve Staunton'],
correctAnswer: 2
},
{
question: 'How many European Cups had Liverpool won up to and including 2007-8?',
choices: ['8', '4', '5', '3'],
correctAnswer: 2
}
];
//grab each of the option divs and assign the 4 options from array
function loadQuestions(questionNumber) {
var sequence = 1;
var questionQuiz = document.getElementById('quiz-question');
questionQuiz.innerHTML = allQuestions[questionNumber].question;
for (var i = 0; i < 4; i++) {
var option = document.getElementById('option' + sequence);
sequence++;
option.innerHTML = allQuestions[questionNumber].choices[i];
}
}
loadQuestions(currentQuestion);
//add evet listeners to each of the options
function optionClickHandler() {
var sequence = 1;
for (var i = 0; i < 4; i++) {
var option = document.getElementById('choice' + sequence);
attr = option.getAttribute("id");
var show = convertOptionToNumber(attr);
console.log(show);
option.addEventListener("click", checkCorrectAnswer);
sequence++;
}
}
optionClickHandler();
function convertOptionToNumber(option) {
if (option === 'choice1') {
option = 0;
} else if (option === 'choice2') {
option = 1;
} else if (option === 'choice3') {
option = 2;
} else if (option === 'choice4') {
option = 3;
}
return parseInt(option);
}
function checkCorrectAnswer() {
var userChoice = convertOptionToNumber(attr);
var correct = allQuestions[currentQuestion].correctAnswer;
parseInt(correct);
if (userChoice === correct) {
alert('Correct!');
} else {
alert('Incorrect!');
}
console.log('The correct answer for question one ' + correct);
}
}
这里是 index.html 文件:
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Quiz</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<div id="wrapper">
<h1>Football Quiz</h1>
<div id="question-number">
<p>You are on Question <span id="count"></span></p>
</div> <!-- end of question counter div -->
<div id="timer">
<p></p>
</div> <!-- end of timer div -->
<p id="quiz-question"></p>
<div id="question-body">
<a id="choice1" href="#"><div class="options">
<p id="option1"></p>
</div></a>
<a id="choice2" href="#"><div class="options">
<p id="option2"></p>
</div></a>
<a id="choice3" href="#"><div class="options">
<p id="option3"></p>
</div></a>
<a id="choice4" href="#"><div class="options">
<p id="option4"></p>
</div></a>
</div> <!-- end of question body div -->
非常感谢您的帮助。
【问题讨论】:
-
快速说明,因为您使用的是 html5 标记:如果 llink 元素是 rel=stylesheet,则不需要“类型”除非它不是 CSS
-
你试过调试吗?
console.log()相关变量,看看它们是什么。对代码中的所有相关值执行此操作,直到找到实际与您的期望不同的地方。 -
这个
parseInt(correct);没有给任何东西赋值。真的什么都没做。 -
一些快速调试表明
userChoice始终为 3。设置一些断点并向后工作,直到找到逻辑错误。
标签: javascript function boolean