我想在网站上集成一个应用程序,潜在的捐赠者可以通过该应用程序找出哪些血型与他们自己的血型兼容。我已经使用下面的代码使用JS,但显然我在某个地方犯了错误!我只是一个初学者,希望它能指导我学习并修复JavaScript中的代码。
var FirstName = prompt("Enter your name: ", "green");
var age = parseInt(prompt("Enter your age: ", "18"));
var bloodType = prompt("Enter your blood type: ");
bloodType = bloodType.toLowerCase();
var res;
if (age >= 18 || age <= 66) {
switch (true) {
case 'o-';
alert("Hello" FirstName ". Since you have blood type"
bloodType "yourself,can donate blood to people with the blood group O-, O +, A-, A +, B-, B +, AB-, AB +.");
break;
case 'o+';
alert("Hello"
FirstName ". Since you have blood type"
bloodType "yourself ,can donate blood to people with the blood group O +, A +, B +, AB +.");
break;
default:
alert('Default case');
break;
.
.
.
.
.
}else {
alert ('Your age does not meet the requirements for giving blood.');
}
}
最佳答案
您需要将bloodType
作为开关值,并将其与switch
statement所需的各种类型进行比较。
var firstName = prompt("Enter your name: ", "green"),
age = parseInt(prompt("Enter your age: ", "18"), 10),
bloodType = prompt("Enter your blood type: ").toLowerCase(),
res;
if (age >= 18 || age <= 66) {
switch (bloodType) {
case 'o-':
alert("Hello " + firstName + ". Since you have blood type " + bloodType + " your can donate blood to people with the blood group O-, O+, A-, A+, B-, B+, AB-, AB+.");
break;
case 'o+':
alert("Hello " + firstName + ". Since you have blood type " + bloodType + " you can donate blood to people with the blood group O+, A+, B+, AB+.");
break;
default:
alert('Default case');
}
} else {
alert('Your age does not meet the requirements for giving blood.');
}