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
Code:
function hpCalc(){
var charClass = document.f1.elements[2].value;
var charLevel = document.f1.elements[3].value;
var charCon = document.f2.elements[5].value;
if (charClass === bbn){
var random = Math.floor(Math.random()*13);
var end = (random()*charLevel())+charCon;
document.f3.elements[1].value = end();}
}
so if the random number is 4 , charLevel is 2 and charCon is 10
(4*2)+10=18
At a glance I can see that you're trying to reference your variables as if they were functions "()" :
Code:
function hpCalc(){
var charClass = document.f1.elements[2].value;
var charLevel = document.f1.elements[3].value;
var charCon = document.f2.elements[5].value;
if (charClass === bbn){
var random = Math.floor(Math.random()*13);
var end = (random()*charLevel())+charCon;
document.f3.elements[1].value = end();}
}
Try removing the brackets and continue from there.
okay i got it to post to the correct box... now my math must be flawed, or perhaps i haven't defined a variable properly.
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());
if (charClass === "brb"){
var end = random*13+1;}
document.f3.elements[0].value = end;}
}
I ran it through 3 different linters and none of them can find any syntax errors
Such as the "end" variable only being defined if charClass equals brb, but an assignment is done anyway using it. You also have one too many closing braces...
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());
if (charClass === "brb"){
var end = random*13+1;}
document.f3.elements[0].value = end;}
}
Some of the variables are no longer being used for your calculation.
actually that is on purpose... later in the code I will have the var. random assigned to different values depending on the charClass variable selection. basically, my thought was like this
Code:
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 === "brb") {
var random = Math.floor(Math.random()) * 12 + 1;
document.f3.elements[0].value = random(); }
else if (charClass === "brd" || charClass === "rog"){
var random = Math.floor(Math.random()) * 6 + 1;
document.f3.elements[0].value = random(); }
else if (charClass === "clr" || charClass === "mnk" ||
charClass === "drd" || charClass === "rgr"){
var random = Math.floor(Math.random()) * 8 + 1;
document.f3.elements[0].value = random();}
}
}
There's actually a lot more charClass possibilities, and I was trying to assign a different var to each one
Yes, I meant to do that. In all the calculations you had you were adding 1 at the end, obviously to prevent the value being zero.
But you will see I defined charValue with a default value of 1, so the class specific values can just be added to that.
I got it!!! the biggest problem was that when I was retrieving values from the form boxes, they were coming in as strings. The final code was.
Code:
function hpCalc() {
//find the clas of the character
var charClass = document.f0.elements[2].value;
//find the level of the character
var charLevel = document.f0.elements[3].value;
//find the constitution mod of the character
var charCon = document.f1.elements[5].value;
//calculate hitpoints
function hitPoints(){
if (charClass === "bbn") {
var start = Math.floor(Math.random()*12+1)}
var mid = start + Number(charCon);
if (mid < Number(charLevel)){result = Nunber(charLevel);}
else {result = mid;}
return result;
}
document.f3.elements[0].value = hitPoints();
}
}
Bookmarks