Can't seem to get this right (Function call) - Javascript -
so i'm trying write app goes through , selects feat , adds html file. when have looks specific element true, goes through statements accordingly. when say, not make requirement, goes through process again (which should), doesn't add html. in fact, says "undefined".
here's code in question. (i've included truncated version of whole code.)
function feats(){ var ft = prompt("write feat here."); var feat; switch (ft) { case 'alert': feat = "<b>alert:</b> +5 initative. cannot surprised while conscious. other creatures don't gain advantage on attack rolls against result of being hidden you."; initiative = initiative + 5; break; case 'defensive duelist': if (dex >=13) { feat = "<b>defensive duelist:</b> when wielding finesse weapon proficient , creature hits melee attack, can use reaction add proficiency bonus ac attack, potentially causing attack miss you." } else { alert("you not have sufficient dexterity feat. try one."); feats(); } break; default: feat = " "; } return feat; } feats() function switch/case statement exists within. should return feat @ end of function, doesn't when dex not greater or equal 13. how have return different feat when function called again.
the problem code not returning function call feats(). reason why necessary, because otherwise logic continues, breaks case structure , returns feat. since variable feat never assigned value, value of undefined. fix this, return function, meaning function return value of callback, in case same function - recursive function.
example: manually set variable dex, since not included in code example.
var dex = 12; function feats() { var ft = prompt("write feat here."); var feat; switch (ft) { case 'alert': feat = "<b>alert:</b> +5 initative. cannot surprised while conscious. other creatures don't gain advantage on attack rolls against result of being hidden you."; initiative = initiative + 5; break; case 'defensive duelist': if (dex >= 13) { feat = "<b>defensive duelist:</b> when wielding finesse weapon proficient , creature hits melee attack, can use reaction add proficiency bonus ac attack, potentially causing attack miss you." } else { alert("you not have sufficient dexterity feat. try one."); return feats(); } break; default: feat = " "; } return feat; }
Comments
Post a Comment