...

View Full Version : Need help with my little script



JS_XenoArcher
08-30-2011, 06:58 AM
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????

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

Philip M
08-30-2011, 07:19 AM
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.

Old Pedant
08-30-2011, 08:59 PM
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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum