当提示 "Did you find anything..." 运行时,提示文本包含 "false" 而不是实际文本

When the prompt "Did you find anything..." runs, the prompt text contains "false" instead of actual text

    var route = 2; var name = prompt("'What is your name soldier?'");

 if(route === 2) {
  if (prompt ("Well,. Did you find anything unusual out on your patrol? (Lie or truth?)".length === 3)) {
  alert("'No sir, nothing unusual.' you say.")
 }
 else
  alert("'Well, I did find a warehouse sir. It was emitting a strange sound, but I thought nothing of it.' you say")
 }

这一行有问题:

if (prompt ("Well,. Did you find anything unusual out on your patrol? (Lie or truth?)".length === 3)) {

它使用布尔值作为 prompt() 的值 - 例如if (prompt(string length ===3) {... 因为 属性 .length is being accessed on the string passed to prompt(),而不是提示中的 return 值.

要检查用户输入的字符串的长度,请在访问 .length 属性 之前移动括号之一,如下所示:

if (prompt ("Well,. Did you find anything unusual out on your patrol? (Lie or truth?)").length === 3) {

在此代码段中尝试一下(单击标有 运行 代码段 的按钮):

 var route = 2; var name = prompt("'What is your name soldier?'");

 if(route === 2) {
  if (prompt ("Well,. Did you find anything unusual out on your patrol? (Lie or truth?)").length === 3) {
  alert("'No sir, nothing unusual.' you say.")
 }
 else
  alert("'Well, I did find a warehouse sir. It was emitting a strange sound, but I thought nothing of it.' you say")
 }