Iul*_*iuc 0 html javascript jsfiddle
我不知道为什么我的代码会停止工作,如果我在上一个函数中添加if语句var checkAnswer = function().如果我删除该声明,代码将再次正常工作.有什么问题呢?这有点奇怪,我不明白.
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 () {}
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
if () {}不是一个有效的if陈述 - 条件体必须是一个表达式.
资料来源:ECMAScript 5规范
例如,以下任何一个都是有效的:
if (true) {}
if (1 < 2) {}
if("") {}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
58 次 |
| 最近记录: |