PDA

View Full Version : String Should Be Number...


Eternity Angel
10-02-2002, 07:10 PM
I am absolutely PUZZLED as to what exactly is going wrong..
I have a button, it simply calls a function, that function being:


function exploring(valued)
{
if (paused == "yes") {
return;
}
if (gameon == "no") {
return;
}
var random = valued*100;
if (random > money) {
alert('You do not have enough Gold to explore '+valued+' Land');
return;
}
random = (valued*20)+100;
if (random > popu) {
alert('You do not have enough Population to explore '+valued+' Land');
return;
}
money = money-(valued*100);
popu = popu-(valued*20);
gold.innerHTML = money;
pop.innerHTML = popu;
if (currexplore == 0) {
currexplore = valued;
currexplorenum = valued*5;
setTimeout("explorer();",1000);
} else {
currexplore = currexplore+valued;
currexplorenum = currexplorenum+(valued*5);
}
edes.innerHTML = currexplore;
erem.innerHTML = currexplorenum;
}


Now, with a whole wack of confusing stuff there, seep through it to the:

if (currexplore == 0) {
currexplore = valued;
currexplorenum = valued*5;
setTimeout("explorer();",1000);
} else {
currexplore = currexplore+valued;
currexplorenum = currexplorenum+(valued*5);
}

Now, what happens is, instead of doing currexplore + valued (which should equal 2 if it already equals one, and you add one [valued]) it does it as if it was a string, it prints out "11" instead of "2". I have NO clue why it is doing this, and it is annoying the heck out of me...

I am guessing it thinks either that currexplore is a string, and not a number, or that valued is...

The button that calls this function is:

<input class="buttons" onfocus="this.blur();" type=button onclick="exploring(building.expnum.value);" value="Explore" name="toexplore">

But I do not think there is anything wrong with the button..

Garadon
10-02-2002, 07:53 PM
try that
currexplore = currexplore+eval(valued);

Roy Sinclair
10-03-2002, 04:12 PM
If it's treating it as a string all you need do is force it to recognize it as a number like this:

currexplore = Number(currexplore)+Number(valued);

Pooh
10-03-2002, 04:25 PM
or..........

<script>
currexplore="12"
valued="5"
currexplore = (currexplore)*1+(valued)*1
alert(typeof currexplore);alert(currexplore)
</script>

mordred
10-03-2002, 05:52 PM
...or

var currexplore = parseInt(currexplore, 10) + parseInt(valued, 10) ;

Eternity Angel
10-03-2002, 07:37 PM
Thank you for the replies!

I am not at home right now, so I cannot try these, but when I get there, I will, and see if I can get it to work.

I have another request though.
How can you tell the length of a number? When I use variable.length, it returns null if the variable is a number, but does return the length of a string.

Once again, thank you.

mordred
10-03-2002, 08:36 PM
Again, you can chose from a small range of possiblities:

var numLength = numVariable.toString().length;

var numLength = String(numVariable).length;

var numLength = (numVariable + "").length;

:)

Eternity Angel
10-03-2002, 10:26 PM
Thank you all!

Everything works fine!