使答案案例不敏感

时间:2016-03-09 03:18:50

标签: javascript

我问用户一个问题,我希望他们的答案不区分大小写。他们是否使用大写字母应该有正确的答案。任何帮助都将不胜感激。

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 个答案:

答案 0 :(得分: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>');
}

答案 1 :(得分:0)

要将字符串转换为全部小写,请使用:

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