html - JavaScript Battle Simulator Failing -


i attempting make battle simulator in prompted user input attacking troops, defending troops, kill rates using prompt() function in javascript. when wrote code html document test it, fails prompt user. realize have written code lot better below using prototypes now, idea came me after completing code. apologize formatting weirdness in code below well. code need debugging below. able me figure out wrong code?

<!doctype html> <html> <head> <title> battle simulator amun </title> </head>  <body>    <script language="javascript">     var armyattackingin;     var armyattackingnumber;     var armydefendingin;     var armydefendingnumber;     //declares variables     armyattackingin=prompt("how many forces in attacking army?");     armyattackingnumber = parseint(armyattackingin);     armydefendingin=prompt("how many forces in defending army?");     armydefendingnumber = parseint(armydefendingin);     //gets number of armies      var morale;     //makes morale factor     var moraleyesorno = prompt("shall morale factor? y/n");     if (moraleyesorno=="y"||moraleyesorno=="yes"||moraleyesorno="yes") {     morale = ((defender/attacker)*3+.3)*100;      if (morale>=100) {         morale = 100;      }     }     else {     morale = 100;     };     //defines morale is.     var killratedefense = parseint(prompt("insert defensive kill rate. default 70.","70");)/100;     var killrateoffense = parseint(prompt("insert offensive kill rate. default 60.","60");)/100;     var battle = function() { //defines kill rate     var armyattackingleft = armyattackingnumber - (armydefendingnumber*killratedefense*(100/morale));     var defendingarmyleft = armydefendingnumber - (armyattackingnumber*killrateoffense*(morale/100)); //gets armies left     if(armyattackingleft<0) {         armyattackingleft = 0     };     if(armydefendingleft<0) {         armydefendinigleft = 0     };     //makes minimum troops left 0     } battle(); document.write("the attacking army " + armyattackingin + "men strong" <br>"); document.write("the defending army " + armydefendingin + "men strong" <br>"); document.write("the morale " + morale + "/%" <br>); document.write("the defensive kill rate " + killratedefensive + "/% , offensive kill rate " + killrateoffensive + "/%" <br>); document.write("there " + (armyattackingnumber-armyattackingleft) + " attackers killed" <br>); document.write("there " + (armydefendingnumber-armydefendingleft) + " defenders killed" <br>); document.write("there " + armyattackingleft + " attacking soldiers survived , there " + armydefendingleft + " defending soldiers survived."); if (armyattackingleft == 0) {     document.write(<br>"the attacking force demolished. died."); }; if (armydefendingleft == 0) {     document.write(<br>"the defending force demolished. died."); }; //writes document   </script> </body> </html> 

have used javascript console of browser before? may find useful debugging tool.

obvious errors prompt() calls: have semicolon in code , causing error "uncaught syntaxerror: missing ) after argument list".

the correct line is:

parseint(prompt("insert defensive kill rate. default 70.","70"))/100; 

your if statement missing '='. doing assignment instead of check, , it's throwing error. should be:

if (moraleyesorno=="y"||moraleyesorno=="yes"||moraleyesorno=="yes") { 

there's typo in 1 of vars:

armydefendinigleft = 0 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -