www.webdeveloper.com
Results 1 to 11 of 11

Thread: Having some trouble with my code

  1. #1
    Join Date
    Jul 2012
    Posts
    7

    Having some trouble with my code

    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

    What's wrong with this code?
    Thanks in advance

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Hi diamotsu,

    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.

  3. #3
    Join Date
    Jul 2012
    Posts
    7
    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

  4. #4
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Using http://www.jslint.com/, there are still some 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.

  5. #5
    Join Date
    Jul 2012
    Posts
    7
    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

  6. #6
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    You could also write it out like this, I think it makes it a little easier to read:

    Code:
    function hpCalc()
    {
    	var
    		charClass = document.f0.elements[2].value,
    		charLevel = document.f0.elements[3].value,
    		charCon   = document.f1.elements[5].value,
    		charValue = 1;
    
    	switch (charClass) {
    
    		case 'brb':
    			charValue += Math.floor(Math.random() * 12);
    			break;
    
    		case 'brd':
    		case 'rog':
    			charValue += Math.floor(Math.random() * 6);
    			break;
    
    		case 'clr':
    		case 'mnk':
    		case 'drd':
    		case 'rgr':
    			charValue += Math.floor(Math.random() * 8);
    			break;
    
    	}
    	document.f3.elements[0].value = charValue;
    }
    I haven't tested the code.

  7. #7
    Join Date
    Jul 2012
    Posts
    7
    just wondering... did you mean to put "+=" on the charValues? if so, what does += mean?

  8. #8
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    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.

    So:

    Code:
    charValue += Math.floor(Math.random() * 6);
    is the same as saying:

    Code:
    charValue = charValue + Math.floor(Math.random() * 6);
    http://www.w3schools.com/js/js_operators.asp

  9. #9
    Join Date
    Jul 2012
    Posts
    7
    I appreciate it. It's still not working yet, but you've done more than enough to help. Thanks

  10. #10
    Join Date
    Jul 2012
    Posts
    7
    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();
    	}
    	}
    Thanks for the help

  11. #11
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Good to hear.

    If you need help with this later on, it would be useful to see the html that interacts with the code as well.


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles