2016-08-20 42 views
-2

以下IF語句不斷運行:「對不起,你錯了。」儘管輸入了正確的答案。這裏有什麼問題?將提示答案轉換爲大寫?

var answer = prompt("What programming language is the name of a gem?"); 

if (answer.toUpperCase() === "ruby") { 
document.write("<p>That is correct</p>"); 
} else { 
document.write("<p>Sorry, you're wrong.</p>"); 
} 

謝謝。

+0

你有小寫的「紅寶石」,所以每次比較都會失敗,因爲你在大寫用戶輸入。 –

+0

更改爲'===「RUBY」'。 –

+0

更改爲:if(answer.toUpperCase()===「RUBY」) – StarsSky

回答

0

由於所有轉換後的情況現在都是大寫,因此您的比較在每種情況下都會失敗。

因此要麼使用toUpperCase與所有上部比較參數或使用toLowerCase與較低的比較參數。

var answer = prompt("What programming language is the name of a gem?"); 

if (answer.toUpperCase() === "RUBY") { 
document.write("<p>That is correct</p>"); 
} else { 
document.write("<p>Sorry, you're wrong.</p>"); 
} 
0

的問題是,你正在運行的

answer.toUpperCase() 

導致被formattet到大寫字母所有的答案。如果你把大寫的「紅寶石」

var answer = prompt("What programming language is the name of a gem?"); 

if (answer.toLowerCase() === "ruby") { 
document.write("<p>That is correct</p>"); 
} else { 
document.write("<p>Sorry, you're wrong.</p>"); 
} 
0

你必須寫

if (answer.toUpperCase() === "RUBY") { 
document.write("<p>That is correct</p>"); 
} 

: 下面的代碼將解決這個問題。