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 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question I can't get my code to work :S

    Hello! I need help with a JavaScript code I've written, it does not work as intended.

    here is the code:

    Code:
    <html>
    <head>
    <script language="JavaScript">
    var exp = "0";
    var lvl = "1";
    
    function getexp() {
    exp = exp + 5;
    document.getElementById(txtexp).value='You have '+ exp +'!';
    if (exp == "20") {
    alert('lvl up!');
    }
    }
    </script>
    
    <TITLE>
    JavaScript test
    </TITLE>
    </head>
    <body>
    <input type="button" name="Button1" value="train" onclick="getexp()">
    <input type="text" name="txtexp" value="0" size="5" disabled>
    
    </body>
    </html>
    This script shall do:
    when you click "train" you get 5 experience, and when experience reaches 20 you advance one level.
    This will also print the current experience in the textbox.

    But nothing of it works.
    I get an error from my debugging programme, "txtexp is not defined". What is wrong? Please help.
    I've checked the code several times, and I can't find the error.

    Thanks in advance,

    NickBomb

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Once you add 5 to exp it becomes a number so
    Code:
    if(exp == "20")
    will always return false. It should be
    Code:
    if(exp == 20)
    I would also make exp and lvl numbers to begin with.

    And as to txtexp you are using this
    Code:
    document.getElementById
    Now look at your code and tell us what is missing. Since this seems like it probably an assignment thats about as much help as I can give. Good luck.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    Once you add 5 to exp it becomes a number so
    Code:
    if(exp == "20")
    will always return false. It should be
    Code:
    if(exp == 20)
    That's not true. That will still evaluate to true even if they have different type. To quickly test this, type this in the address bar and you will see that it will alert true.
    Code:
    javascript:alert(("20"==20)) //true
    To make a strict comparison, you need to use the strict equality sign ===.
    Code:
    javascript:alert(("20"===20)) //false
    And when you add a number to a string, the result is still a string not a number. If one operand is a string and the other is a number, it will perform concantenation not addition.

    The solution is this:
    Code:
    document.getElementById('txtexp').value='You have '+ exp +'!';
    ...
    <input type="text" name="txtexp" id="txtexp" value="0" size="5" disabled>
    And of course exp should be defined as a number to perform correct addition.
    Last edited by glenngv; 06-12-2007 at 09:16 PM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks.
    But it still gets some errors.


    Code:
    <html>
    <head>
    <script language="JavaScript">
    var exp = 0;
    var lvl = 1;
    
    function getexp() {
    exp = exp + 5;
    document.getElementById(txtexp).value='You have '+ exp +'!';
    if (exp === 20) {
    alert('lvl up!');
    }
    }
    </script>
    
    <TITLE>
    JavaScript test
    </TITLE>
    </head>
    <body>
    <input type="button" name="Button1" value="train" onclick="getexp()">
    <input type="text" id="txtexp" name="txtexp" value="0" size="5" disabled>
    
    </body>
    </html>
    Error:
    "document.getElementById(txtexp) has no properties"


    This is far to advanced for me :S

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Check my post again. It has quotes in the 'txtexp'.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #6
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yey!
    Thank you very much ^^

  • #7
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oooooh noes!
    I've encountered another problem...

    The function I've written to make you able to attack does'nt work, either

    I guess it's just my lack of knowledge, but can you help me out here aswell?

    First, I made a function that randomizes a monster that you should fight and it's hp. This one works.

    Code:
    function searchforenemies() {
    	// random
    	var r=Math.random()
    	if (r<0.1)              // 1
    	{
    	var monsternamn = "Rat"
    	document.getElementById('txtmonsternamn').value=monsternamn;
    	var monsterhp = 10
    	document.getElementById('txtmonsterhp').value=monsterhp;
    	}
    	else if (r>0.1 && r<0.3) //2
    	{
    	var monsternamn = "Toad"
    	document.getElementById('txtmonsternamn').value=monsternamn;
    	var monsterhp = 7
    	document.getElementById('txtmonsterhp').value=monsterhp;
    	}
    
    // ... 
    // all the way up to 9 then
    
    
    document.getElementById('searchforenemies').disabled=true;
    That code randomizes a monster name, and writes it in the "txtmonsternamn" textbox. And with the name, it follows the monsters HP, that is written in the "txtmonsterhp" textbox.
    Then the "search for enemies"-button is disabled.

    But here comes the tricky part (atleast for me)

    Code:
    function anfall() {
    	// random
    	var a=Math.random()
    	if (a<0.1)              // 1
    	{
    	var monsterhp = monsterhp - 2
    	document.getElementById('txtmonsterhp').value=monsterhp;
    	}
    	else if (a>0.1 && a<0.3) //2
    	{
    	var monsterhp = monsterhp - 1
    	document.getElementById('txtmonsterhp').value=monsterhp;	
    	}
    //...
    //all the way up to nine
    I've assigned the function "anfall()" to a button.

    But, when I click the button, the monster HP textbox just says "NaN".


    Please help, if you get my problem.

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    var monsterhp = monsterhp - 2
    Because monsterhp is undefined. You're declaring monsterhp variable to have a value of monsterhp - 2. That red variable is not defined in the previous lines. So it's like you're doing this which results to NaN.
    Code:
    var monsterhp = undefined - 2
    What do you really want to do?
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    I thought we had already dealt with this at

    http://www.codingforums.com/showthread.php?t=116264

    It would be better not to have two threads going at the same time.

    Last edited by Philip M; 06-13-2007 at 04:57 PM.

  • #10
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry about the misunderstanding.
    I created a new thread, because I thought you did not know that I replied to this thread with another question.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    In future please stick to just one thread per topic, and have a look at the posting guidelines re silly thread titles. The title you choose is supposed to assist others in future who are searching for assistance on the topic, so something like "My code does not work" is not helpful.


  •  

    Posting Permissions

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