How do I get a status to only update when necessary in javascript? -
i'm trying create 5th edition d&d character generator app (i know, super nerdy). i'm trying have questions asked based on level character at. want update information instead of reasking of same information. example, if character ranger @ level 1, ask favored terrain , favored enemies. if have character go level 2, reask questions , ask next question related level 2. how have update information gathered level 2 , on?
i believe question answer how update experience points , not have ask same questions on , on again. if not, next question.
thanks.
here sample of code in question:
if (level >= 1){ var favterrain1 = prompt("name favorite terrain."); var favenemy1 = prompt("name favorite enemy type or 2 humanoid races. (if select latter, please separate comma.)"); document.getelementbyid("level1").innerhtml = "<h4><b> favored terrain: </b>" + favterrain1 + "<br><b>favored enemy(enemies): </b>" + favenemy1 + "</h4>"; } if (level >= 2) { var lv2 = +prompt("choose fighting style: 1. archery 2. defense 3. dueling 4. two-weapon fighting"); var level2; switch(lv2){ case 1: level2 = "<b>archery</b> gain +2 bonus attack rolls make ranged weapons."; break; case 2: level2 = "<b>defense</b> while wearing armor, gain +1 armor class."; armorclass = armorclass + 1; break; case 3: level2 = "</b>dueling</b> when wielding melee weapon , no other weapons, gain +2 bonus damage rolls weapon."; break; case 4: level2 = "<b>two-weapon fighting</b> when engage in two-weapon fighting, can add ability modifier damage of second attack."; break; } } else { level2= " "; }
it ask both level1 , level2 sets of questions if upgrade level 1 2.
by building character you're creating object in classic way: character has number of attributes build level up.
to prompt user questions necessary, can check see if player
object has properties gain @ level hasownproperty function.
so use
var player = new object(); player.favoredterrain = "jungle"; if (!player.hasownproperty('favoredmount')){ //whatever code ask favored mount }; if (!player.hasownproperty('favoredterrain')){ //whatever code in here won't run because has favored terrain already! };
note number of frameworks make stuff much easier on large list of values e.g. angular's test see if x value populated , hide/unhide questions. alternatively, jquery make easy select , hide blocks of questions have answered. since question 'how do javascript' tried give 'pure' version
Comments
Post a Comment