似乎无法做到这一点(函数调用) - Javascript

时间:2016-01-05 21:11:33

标签: javascript

所以我试图编写一个经过并选择专长的应用程序并将其添加到html文件中。当我有一些寻找特定元素的东西时,它会相应地通过这些陈述。但是,当说,它没有提出要求时,它会再次通过该过程(它应该),但是然后不会将它添加到html中。事实上,它只是说" undefined"。

这是有问题的代码。 (我已经包含了整个代码的截断版本。)

function feats(){
    var ft = prompt("Write your feat here.");
    var feat;
    switch (ft) {
        case 'alert':
            feat = "<b>Alert:</b> +5 initative. You cannot be surprised while you are conscious. Other creatures don't gain advantage on attack rolls against you as a result of being hidden from you.";
            initiative = initiative + 5;
            break;  
case 'defensive duelist':
        if (dex >=13) {
            feat = "<b>Defensive Duelist:</b> When you are wielding a finesse weapon with which you are proficient and another creature hits you with a melee attack, you can use your reaction to add your proficiency bonus to your AC for that attack, potentially causing the attack to miss you."
        } else {
            alert("You do not have sufficient dexterity for this feat. Try another one.");
            feats();
        }
        break;
        default:
            feat = " ";
    } 
    return feat;
}

feats()是此开关/案例语句所在的函数。它应该在函数结束时返回专长,但是当dex不大于或等于13时它不会返回。那么当再次调用该函数时,如何让它返回另一个专长。

3 个答案:

答案 0 :(得分:1)

您的代码存在的问题是您没有返回函数调用feats()。这是必要的原因,因为否则逻辑继续,打破案例结构和returns feat。由于变量feat永远不会赋值,因此您获得undefined的值。要解决这个问题,你需要返回函数,这意味着函数将返回回调的值,在这种情况下是相同的函数 - 递归函数。

示例:我手动设置变量dex,因为代码示例中未包含该变量。

var dex = 12;

function feats() {
    var ft = prompt("Write your feat here.");
    var feat;
    switch (ft) {
        case 'alert':
            feat = "<b>Alert:</b> +5 initative. You cannot be surprised while you are conscious. Other creatures don't gain advantage on attack rolls against you as a result of being hidden from you.";
            initiative = initiative + 5;
            break;
        case 'defensive duelist':
            if (dex >= 13) {
                feat = "<b>Defensive Duelist:</b> When you are wielding a finesse weapon with which you are proficient and another creature hits you with a melee attack, you can use your reaction to add your proficiency bonus to your AC for that attack, potentially causing the attack to miss you."
            } else {
                alert("You do not have sufficient dexterity for this feat. Try another one.");
                return feats();
            }
            break;
        default:
            feat = " ";
    }
    return feat;
}

答案 1 :(得分:0)

您只需更改行

即可
feats()

else语句中

return feats()

但重新组织可能更简单,这取决于你对函数中多个return语句的感受。

function feats(){
    var ft = prompt("Write your feat here.");
    switch (ft) {
        case 'alert':
            initiative = initiative + 5;
            return "<b>Alert:</b>...";
        case 'defensive duelist':
            if (dex >=13) {
                return "<b>Defensive Duelist:</b>...";
            } else {
                alert("You do not have...");
                return feats();
            }
        default:
            return " ";
    } 
}
对我来说,至少,这读起来要好得多。

答案 2 :(得分:-1)

feats();放在ifelse声明之外。

相关问题