PDA

View Full Version : Having some trouble with my code

diamotsu
Jul 31st, 2012, 04:49 AM
All variables (except charClass) are numbers
If charClass is a specific option in the dropdown, then I need this equation to result

((random # between 1-12)*charLevel)+charCon

Here's my code

function hpCalc() {
var charClass = document.f0.elements[2].value;
var charLevel = document.f0.elements[3].value;
var charCon = document.f1.elements[5].value;
var random = Math.floor(Math.random());
function hitPoints(){
if (charClass === "bbn") {
var clas = random * 12 + 1;
var start = clas * charLevel;
var end = start + charCon;
return end;
}
}
document.f3.elements[0].value = hitPoints();
}

I don't know what's wrong with the code.

diamotsu
Jul 31st, 2012, 05:28 AM
okay i figured that out. New problem
sometimes the charCon variable will be a negative integer. and with the code that I have up for the math

var end = start + charCon;
if start = 2 and charCon is -1, the result is 2-1
anybody have any ideas?

Old Pedant
Jul 31st, 2012, 06:09 AM
Your random code is so much hash.

function hpCalc() {
var charClass = document.f0.elements[2].value;
var charLevel = document.f0.elements[3].value;
var charCon = document.f1.elements[5].value;
if (charClass === "bbn")
{
document.f3.elements[0].value =
charCon + charLevel * Math.floor( 1 + Math.random() * 12 );
}
}

But I don't know what you want to do--if anything--about charLevel < 0.