在 if 语句中处理变量时如何忽略大小写 - Javascript
How to ignore case when dealing with a variable in an if statement - Javascript
我是 Javascript 的完全初学者,并尝试使用尽可能简单的代码。在 if 语句中使用来自提示的变量时,存在一些关于区分大小写的问题。这是有问题的代码。假设输入正好是 'French'
,它可以完美运行
if (number => 1 && number <= 30) {
var lang= prompt ("Which language do you want to translate into, French or German?");
}
while(lang != "French" && lang != "french" && lang != "FRENCH" && lang != "German" && lang != "german" && lang != "GERMAN" ) {
alert("Only French or German is allowed");
var lang= prompt ("Which language do you want to translate into, French or German?");
}
if (number == 1 && lang == "French") {
alert("The translation is " +frenchNumbers[1]);
}
if (number == 2 && lang == "French") {
alert("The translation is " +frenchNumbers[2]);
}
if (number == 3 && lang == "French") {
alert("The translation is " +frenchNumbers[3]);
}
我希望 'if' 语句在用户输入 'french' 或 'FRENCH' 时也能正常工作。我尝试使用下一段代码,但它从头开始循环显示警报,这不是我想要的。例如,如果我要输入“3”和 'FRENCH',我会首先收到“1”和“2”的警报。
if (number == 1 && lang == "French" || lang == "french" || lang == "FRENCH") {
alert("The translation is " +frenchNumbers[1]);
}
if (number == 2 && lang == "French" || lang == "french" || lang == "FRENCH") {
alert("The translation is " +frenchNumbers[2]);
}
if (number == 3 && lang == "French" || lang == "french" || lang == "FRENCH") {
alert("The translation is " +frenchNumbers[3]);
}
我确信有一个简单的解决方法,尝试解决这个问题有点疯狂,因为我看过的所有信息都远远超出了我的技能水平
任何帮助都会很棒
谢谢,布拉德
正如 Chris Baker 所解释的,您可以使用 .toLowerCase()
。您需要调整的代码如下
while(lang.toLowerCase() != "french" && lang.toLowerCase() != "german") {
...
if (number == 1 && lang.toLowerCase() == "french") {
...
我是 Javascript 的完全初学者,并尝试使用尽可能简单的代码。在 if 语句中使用来自提示的变量时,存在一些关于区分大小写的问题。这是有问题的代码。假设输入正好是 'French'
,它可以完美运行if (number => 1 && number <= 30) {
var lang= prompt ("Which language do you want to translate into, French or German?");
}
while(lang != "French" && lang != "french" && lang != "FRENCH" && lang != "German" && lang != "german" && lang != "GERMAN" ) {
alert("Only French or German is allowed");
var lang= prompt ("Which language do you want to translate into, French or German?");
}
if (number == 1 && lang == "French") {
alert("The translation is " +frenchNumbers[1]);
}
if (number == 2 && lang == "French") {
alert("The translation is " +frenchNumbers[2]);
}
if (number == 3 && lang == "French") {
alert("The translation is " +frenchNumbers[3]);
}
我希望 'if' 语句在用户输入 'french' 或 'FRENCH' 时也能正常工作。我尝试使用下一段代码,但它从头开始循环显示警报,这不是我想要的。例如,如果我要输入“3”和 'FRENCH',我会首先收到“1”和“2”的警报。
if (number == 1 && lang == "French" || lang == "french" || lang == "FRENCH") {
alert("The translation is " +frenchNumbers[1]);
}
if (number == 2 && lang == "French" || lang == "french" || lang == "FRENCH") {
alert("The translation is " +frenchNumbers[2]);
}
if (number == 3 && lang == "French" || lang == "french" || lang == "FRENCH") {
alert("The translation is " +frenchNumbers[3]);
}
我确信有一个简单的解决方法,尝试解决这个问题有点疯狂,因为我看过的所有信息都远远超出了我的技能水平
任何帮助都会很棒
谢谢,布拉德
正如 Chris Baker 所解释的,您可以使用 .toLowerCase()
。您需要调整的代码如下
while(lang.toLowerCase() != "french" && lang.toLowerCase() != "german") {
...
if (number == 1 && lang.toLowerCase() == "french") {
...