如果我在上一个函数var checkAnswer = function()
中添加if语句,我不知道为什么我的代码会停止工作。如果我删除该声明,代码将再次正常工作。有什么问题呢?它有点奇怪,我不明白。
Array.prototype.random = function(length) {
return this[Math.floor(Math.random() * length)];
};
var country = [{
name: "romaniei",
capital: "bucuresti"
}, {
name: "bulgariei",
capital: "sofia"
}],
questions = [
"What is the capital of ",
" is the capital of what country?"
]
document.querySelector('input').onclick = function() {
var q,
chosen_country = country.random(country.length),
chosen_question = questions.random(questions.length);
if (chosen_question == questions[0]) {
q = chosen_question + chosen_country.name + "?";
} else if (chosen_question == questions[1]) {
q = chosen_country.capital + chosen_question;
}
document.querySelector('#que').innerHTML = q;
}
var checkAnswer = function() {
var answer = document.myform.answ.value;
if () {}
}
<form name="myform">
<input type="button" value="Generate question">
<div id="que">Intrebare:</div>
<input type="text" id="answ">
<input type="button" value="ok">
</form>
答案 0 :(得分:5)
if () {}
不是有效的if
语句 - 条件体必须是表达式。
例如,其中任何一个都有效:
if (true) {}
if (1 < 2) {}
if("") {}