2016-03-09 75 views
0

我在問用戶一個問題,我希望他們的答案不區分大小寫。無論他們是否使用大寫字母,他們都應該有正確的答案。任何幫助,這將不勝感激。製作答案不區分大小寫

var name = prompt('Enter your name'); 
var correctAnswers = 0; 
var questions = 15; 

document.write('Okay '+name+' here is Question 1: What does html stand for?: HyperText Markup Language, High Track Making Language, High Tech Making Language'); 
var answer = prompt('Okay '+name+' here is Question 1: What does html stand for?: HyperText Markup Language, High Track Making Language, High Tech Making Language'); 

if (answer === 'HyperText Markup Language') { 
    console.log(correctAnswers = correctAnswers + 1); 
    console.log('That is correct!'); 
    document.write('<div>That is correct!</div>'); 
} else { 
    console.log('Sorry that is wrong. The correct answer is HyperText Markup Language.'); 
    document.write('<div>Sorry that is wrong. The correct answer is HyperText Markup Language.</div>'); 
} 
+2

使用'toLowerCase() '將字符串轉換爲小寫,並與全部小寫的答案進行比較。 'answer.toLowerCase()==='超文本標記語言' – Tushar

回答

2

可以使用toLowerCase();toUpperCase();

if (answer.toLowerCase() === ('HyperText Markup Language').toLowerCase()) { 
    console.log(correctAnswers = correctAnswers + 1); 
    console.log('That is correct!'); 
    document.write('<div>That is correct!</div>'); 
} else { 
    console.log('Sorry that is wrong. The correct answer is HyperText Markup Language.'); 
    document.write('<div>Sorry that is wrong. The correct answer is HyperText Markup Language.</div>'); 
} 
0

將字符串轉換爲全部小寫,使用的答案轉換爲小寫或大寫:

if (answer.toLowerCase() === "string") { 
    // code here 
}