gpt4 book ai didi

javascript - 如何使我的用户响应不区分大小写?

转载 作者:行者123 更新时间:2023-11-28 18:09:41 24 4
gpt4 key购买 nike

我碰壁了。我正在尝试编写代码,以便当用户在提示框中输入响应时,他们的答案不需要区分大小写才能正确。有什么建议吗?

这是我的代码:

var questions = [
["What solar system do we live in?", "Milky Way"],
["How many ounces are in a cup?", "16"],
["What type of cellphone is currently the most popular throughout the world?", "iPhone"],
["What's Chicago's tallest building?", "Willis Tower"],
["What is the name of the Chicago White Sox' new stadium?", "Guaranteed Rate"]
];

var correctAnswers = 0;
var question;
var answer;
var response;
var html;
var correct = [];
var wrong = [];

function print(message) {
var outputDiv = document.getElementById("output");
outputDiv.innerHTML = message;
}

function buildList(arr) {
var listHTML = "<ol>";
for(var i=0;i<arr.length;i++) {
listHTML += "<li>" + arr[i] + "</li>";
}
listHTML += "</ol>";
return listHTML;
}

for(var i=0;i<questions.length;i++) {
question = questions[i][0];
answer = questions[i][1];
response = prompt(question);
if(response === answer) {
correctAnswers += 1;
correct.push(question);
} else {
wrong.push(question);
}
}

html = "You got " + correctAnswers + " question(s) right.";
html += "<h2>You got these questions correct:</h2>";
html += buildList(correct);
html += "<h2>You got these questions wrong:</h2>";
html += buildList(wrong);
print(html);

最佳答案

只需将它们与全部小写或全部大写进行比较:

if(response.toLowerCase() === answer.toLowerCase()) {

这样你就可以让用户以任何他想要的方式编写它,但仍然可以正确验证

关于javascript - 如何使我的用户响应不区分大小写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41877773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com