2017-01-10 41 views
0

當提示「你有沒有發現......」運行,提示文本中包含的,而不是實際的文本「假」

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

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

回答

1

問題是這一行:

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

它使用一個布爾值作爲prompt()的值 - 例如如果(提示(字符串長度 === 3){...由於屬性.length被傳遞到prompt()字符串訪問,而不是從提示的返回值。

要檢查的長度用戶輸入的字符串,將括號中的一個.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?'"); 
 

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

+0

是的,我發現不久後我發佈了這個問題......但是,無論如何感謝您的幫助! – Bestlogo56

相關問題