Web Analytics Made Easy -
StatCounter Need help with my little script - CodingForum

Announcement

Collapse
No announcement yet.

Need help with my little script

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help with my little script

    Hi everyone,
    I just wrote a little noob script and JSL tester says no errors but I think there's some kind of problem.....can you help me fix the problem????
    Code:
    function doattack(baseatk, wepatk, wepcritmax, oppdef, oppdeffactor, oppextradefmax, opphealth, oppname) 	{ 
               var OpponentHealth = opphealth;
               var OpponentName = oppname;
    	   var BaseATK = baseatk;
    	   var WeaponATK = wepatk;
    	   var WeaponCriticalMax = "100+Math.floor(Math.random()*wepcritmax+1)";
               var ActualWeaponATK = "WeaponATK * WeaponCriticalMax / 100";
    	   var OpponentDEF = oppdef; 
               var DEFfactor = oppdeffactor; //defensefactor: used to multiply preoppdef to calculate actualoppdef so more in number the better rmbr that deffactor must be less than 1 
               var DEFCriticalMax = "100+Math.floor(Math.random()*oppextradefmax+1)"; 
               var PreOpponentDEF = "OpponentDEF * DEFCriticalMax / 100";
    	   var ActualOpponentDEF = "PreOpponentDEF * DEFfactor";
               var DamageDone = "BaseATK + ActualWeaponATK";
               var OVRDamageDone = "DamageDone * ActualOpponentDEF / 100";
               document.write("You just inflicted " + OVRDamageDone + " damage to " + OpponentName + "!" + "<br>");
               var NewOpponentHealth = "OpponentHealth - OVRDamageDone";
    	   if (NewOpponentHealth < 0) 
                 {
    	     document.write(OpponentName + " is dead!" + "<br>"); 
       	     break;
                 }
               else 
                 { 
                 doattack(BaseATK, WeaponATK, WeaponCriticalMax, OpponentDEF, DEFfactor, DEFCriticalMax, NewOpponentHealth, OpponentName);
                 }
           } 
    	doattack(5, 50, 25, 6, 0.22, 10, 200, RedBabyDragon);
    GREAT THANKS
    Last edited by JS_XenoArcher; Aug 30, 2011, 02:06 AM.

  • #2
    var WeaponCriticalMax = "100+Math.floor(Math.random()*wepcritmax+1)";
    var ActualWeaponATK = "WeaponATK * WeaponCriticalMax / 100";

    By enclosing the values in quotes they become literal strings, like "Hello World". Note that this is not a syntax error.

    But RedBabyDragon is a literal string value so needs to be in quotes. Without quotes you are trying to pass the value of a variable named RedBabyDragon.

    You cannot have a break statement outside a loop. This is very definitely an error. Delete it.

    You script sets up an endless loop by calling the function over again. Delete the else statement.


    document.write("You just inflicted " + OVRDamageDone + " damage to " + OpponentName + "!" + "<br>");

    document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

    You should use DOM methods -
    <div id = "myMessage"></div>
    and in script
    document.getElementById("myMessage").innerHTML = OpponentName + " is dead!";

    Quizmaster: The trumpet call of Reveille is a signal to soldiers to do what?
    Contestant: Start shooting.
    Last edited by Philip M; Aug 30, 2011, 03:13 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

    Comment


    • #3
      Originally posted by Philip M View Post
      Quizmaster: The trumpet call of Reveille is a signal to soldiers to do what?
      Contestant: Start shooting.
      What's wrong with that answer? I often felt like shooting the *^!&#! bugler!
      Be yourself. No one else is as qualified.

      Comment

      Working...
      X
      😀
      🥰
      🤢
      😎
      😡
      👍
      👎