如果我想添加更多代码,js 将无法工作

Js won't work if I want to add more code

我不知道为什么如果我在最后一个函数中添加 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 () {}
}
<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>

if () {} 不是有效的 if 语句 - 条件体必须是表达式。

来源:ECMAScript 5 spec

例如,以下任何一个都是有效的:

if (true) {}
if (1 < 2) {}
if("") {}