Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 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; 08-30-2011 at 07:06 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,173
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    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; 08-30-2011 at 08: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.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,720
    Thanks
    80
    Thanked 4,515 Times in 4,479 Posts
    Quote 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!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •