...

View Full Version : I can't get my code to work :S



NickBomb
06-12-2007, 09:20 PM
Hello! I need help with a JavaScript code I've written, it does not work as intended.

here is the 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

_Aerospace_Eng_
06-12-2007, 09:34 PM
Once you add 5 to exp it becomes a number so

if(exp == "20")
will always return false. It should be

if(exp == 20)
I would also make exp and lvl numbers to begin with.

And as to txtexp you are using this

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.

glenngv
06-12-2007, 10:08 PM
Once you add 5 to exp it becomes a number so

if(exp == "20")
will always return false. It should be

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.

javascript:alert(("20"==20)) //true

To make a strict comparison, you need to use the strict equality sign ===.

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:


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.

NickBomb
06-13-2007, 07:01 AM
Thanks.
But it still gets some errors.




<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

glenngv
06-13-2007, 07:09 AM
Check my post again. It has quotes in the 'txtexp'.

NickBomb
06-13-2007, 07:31 AM
Yey! :D
Thank you very much ^^

NickBomb
06-13-2007, 08:40 AM
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.



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)



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.

glenngv
06-13-2007, 04:59 PM
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.

var monsterhp = undefined - 2
What do you really want to do?

Philip M
06-13-2007, 05:53 PM
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.

:confused:

NickBomb
06-13-2007, 06:48 PM
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.

Philip M
06-13-2007, 07:35 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum