Reputation: 13
I've been looking around and I can't seem to find out why this is happening or how to fix it. I've been trying different things for about 2 days, now its time to ask for help before I rip the rest of my hair out (I'm new to Java)
I know the code is very long winded at the moment, but its like that for readability to see why its not working for me.
function getBaB()
{
var BaB=0;
var theForm = document.forms["prrform"];
var basebab = theForm.elements["charbab"];
BaB = basebab.value;
return BaB;
}
var armour_types = new Array();
//This is the bit that doesn't work
armour_types["Heavy"] = (getBaB.value) ;
//It just returns undefined, and if I parseInt then I get NaN. I just can't see why it isn't defined.
armour_types["Medium"] = 10;
armour_types["Light"] = 2;
armour_types["Adamantine"] = 10;
armour_types["Mithral"] = 5;
armour_types["Unarmoured"] = 0;
function calculateTotal()
{
var TotalPRR = getArmourType();
var divobj = document.getElementById('totalPRR');
divobj.style.display='block';
divobj.innerHTML = "Total PRR "+TotalPRR;
}
function hideTotal()
{
var divobj = document.getElementById('totalPRR');
divobj.style.display='none';
}
I can post the HTML if needed.
Thanks in advance :
Upvotes: 1
Views: 277
Reputation: 1199
Kirubhananth Chellam is correct with his answer:
armour_types["Heavy"] = getBaB();
But as you're new to Javascript (Java has a very different syntax) I'll try an explain where you're going abit wrong an see if I can help you further.
Javascript is loosely typed - and this is key to the entire language. Consider...
var x = 0;
... as you imagine assigns value 0 to x. Simple but a key starting point as the assignment...
x = "hello"
... is not so simple to understand. In strongly typed languages, this would return a type error however is Javasript, this is perfectly acceptable. As is...
x = function(){
var y = 0;
return y;
}
...even after x has been assigned a numeric value.
The reason this is important as when Javascript is interpreted at runtime, function declarations such as...
function x(){
...
}
... are actually interpreted in the same way as variable assignments...ie....
var x = function(){
...
}
...which can result in odd behaviour if you overload the variable x (see Hoisting http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html).
Returning to your question, the call to getBaB.value means you're calling for getBaB as a variable assigned an object with an attribute called 'value'.
In your code, Javascript will return the function definition (which is technically an object) as the value getBaB which doesn't have a 'value' attribute and thus undefined.
Note: Attempting to Parse undefined as an int oddly returns NaN (not undefined).
As for your function...
function getBaB()
{
var BaB=0;
var theForm = document.forms["prrform"];
var basebab = theForm.elements["charbab"];
BaB = basebab.value;
return BaB;
}
return BaB
does not return an object, though you might think it does. Returning to javascript being loosely typed; BaB = basebab.value;
would mean BaB is a assigned a numeric value and not an object with a 'value' attribule.
Interestingly, getBaB() could still return 'undefined' if theForm.elements["charbab"];
is not found and 'null' if a value is not set in 'charbab' as it overrides BaB = 0 (loosely typed ;)).
As such I'd suggest you make sure basebab returns a logical value to ensure your code behaves reliably with a quick if statement!
Hope this helps.
GW
Upvotes: 1
Reputation: 72
First the the getBAB() must return an array. I have an example from your code as follows
function BlockID() {
var IDs = new Array();
IDs[0] = "a";
IDs[1] = "b";
IDs[2] = "c";
IDs[3] = "d";
return IDs;
}
var isReady = true;
function doSaveAs(){
var armour_types = new Array();
armour_types["Heavy"] = BlockID();
}
<body>
<input type="button" onClick="doSaveAs();">click</input>
</body>
Upvotes: 0
Reputation: 72
Your method calling is wrong. Just call the method as methodname();
So, In your code
armour_types["Heavy"] = getBaB();
will work i guess.
Upvotes: 0