我想在网站上集成一个应用程序,潜在的捐赠者可以通过该应用程序找出哪些血型与他们自己的血型兼容。我已经使用 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.');
}
关于javascript - JavaScript 中的血型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58363679/