使答案不区分大小写

Making Answers Case Insensitive

本文关键字:大小写 不区 答案      更新时间:2023-09-26

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

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>');
}
您可以使用

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>');
}

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

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