javascript - JavaScript 中的血型

标签 javascript if-statement switch-statement

我想在网站上集成一个应用程序,潜在的捐赠者可以通过该应用程序找出哪些血型与他们自己的血型兼容。我已经使用 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/

相关文章:

javascript - 迭代多个 JSON 而不是仅一个

c - if else 里面 switch case c 语言

javascript - 替换 AJAX 调用中的 'null' 值

javascript - 这是什么 hr = hr>=12 ? hr-12 : hr; means?

javascript - 使用继承时的 BabelJS 类排序

java - 如果条件语句错误“存在编码标准违规(Java)

javascript - meteor 模板,检查值是否等于字符串

Java Scanner if/else 和 while 语句

c# - 非常量开关的替代方案

c - c 中的 switch case 语句没有给出结果