Click to See Complete Forum and Search --> : Why do I get NaN?
Firthy
12-09-2002, 03:03 PM
The problem code is as follows:
var CBA = 0
for (var AddBA = 0; AddBA < NoBeds; AddBA ++)
{
CBA = CBA + PropTaxBA[AddBA]
}
PropTaxBA is an array containing a set of numbers defined earlier in the program.
I want to add the numbers in PropTaxBA together and store them in CBA.
But CBA ends up as NaN.
What have I done wrong?
Zach Elfers
12-09-2002, 03:07 PM
I don't understand because you are only showing a segment of the script. It would help if you showed the whole script. NaN means Not a Number, so it sounds like there is something wrong with the math.
Firthy
12-09-2002, 05:45 PM
The script in full:
<SCRIPT>
/*
This is the initial version of my assignment script.
At the moment it's just a basic program.
*/
//Get customer details first
var CustName = prompt("Please enter the customer's name");
var CustAddy = prompt("Please enter the customer's address");
var CustSex = prompt("Please enter the customer's sex","Male");
var CustYOB = parseInt(prompt("Please enter the customer's year of birth"));
//Now get the house details
var PropAddy = prompt("Please enter the property address");
var NoBeds = parseInt(prompt("Please enter the number of bedrooms",1));
var PropType = prompt("Please enter the type of the property");
var PropPrice = parseInt(prompt("Please enter the selling price of the property"));
while ((CTB != "A") && (CTB != "B") && (CTB != "C"))
var CTB = prompt("Please enter the Council Tax band of the property");
//Calculate Council Tax
if ((PropPrice >= 200000) && (CTB == "A"))
{var CT = 0.6}
else
if ((PropPrice < 200000) && (CTB == "A"))
{var CT = 0.5}
else
if ((PropPrice >= 150000) && (CTB == "B"))
{var CT = 0.35}
else
if ((PropPrice < 150000) && (CTB == "B"))
{var CT = 0.4}
else
if ((PropPrice >= 100000) && (CTB == "C"))
{var CT = 0.25}
else
if ((PropPrice < 100000) && (CTB == "C"))
{var CT = 0.15}
else
alert("Sumthin' ain't right, foo'!");
//Calculate Property Tax
var PTBA = new Array (NoBeds)
for (var BedArea = 0; BedArea < NoBeds; BedArea ++)
{
var BedLength = parseFloat(prompt("Please enter the length of bedroom " + (BedArea + 1)));
var BedWidth = parseFloat(prompt("Please enter the width of bedroom " + (BedArea + 1)));
var BA = (BedLength*BedWidth);
PTBA [BedArea] = BA
}
if (NoBeds > 1)
{
var PropTaxBA = new Array (NoBeds - 1)
var Pos = 0
var Trans = 0
var PropTaxBigRoom = PTBA [Pos]
for (Pos = 1; Pos < NoBeds; Pos ++)
{
if (PropTaxBigRoom > PTBA [Pos])
{ PropTaxBA [(Pos - 1)] = PTBA [Pos] }
else
{ Trans = PropTaxBigRoom
PropTaxBigRoom = PTBA [Pos]
PropTaxBA [(Pos - 1)] = Trans
}
}
}
else
{var PropTaxBA = PTBA}
//Display output
document.write("<H1>" + "Calculation of Property and Council Tax:" + "</H1><BR>");
document.write("<B>" + "CUSTOMER NAME: " + "</B>" + CustName + "<BR>");
document.write("<B>" + "CUSTOMER ADDRESS: " + "</B>" + CustAddy + "<BR>");
document.write("<B>" + "CUSTOMER'S SEX: " + "</B>" + CustSex + "<BR>");
document.write("<B>" + "CUSTOMER'S YEAR OF BIRTH: " + "</B>" + CustYOB + "<BR>");
document.write("<HR>");
document.write("<B>" + "ADDRESS OF PROPERTY: " + "</B>" + PropAddy + "<BR>");
document.write("<B>" + "NUMBER OF BEDROOMS IN PROPERTY: " + "</B>" + NoBeds + "<BR>");
document.write("<B>" + "TYPE OF PROPERTY: " + "</B>" + PropType + "<BR>");
document.write("<B>" + "PROPERTY PRICE: " + "</B>" + "£" + PropPrice + "<BR>");
document.write("<HR>");
document.write("<B>" + "COUNCIL TAX BAND: " + "</B>" + CTB + "<BR>");
document.write("<B>" + "COUNCIL TAX: " + "</B>" + CT + "%" + "<BR>");
document.write("<HR>");
document.write("<B>" + "TOTAL AREA OF BEDROOMS IN PROPERTY: " + "</B>" + PropTaxBA + "<BR>" + CBA + "<BR>");
//document.write("<B>" + "PROPERTY TAX: " + "</B>" + "£" + TPT + "<BR>");
//Test Arrays
document.write("Contents of array PTBA = " + PTBA + "<BR>");
document.write("Contents of array PropTaxBA = " + PropTaxBA + "<BR>");
document.write("Contents of variable PropTaxBigRoom = " + PropTaxBigRoom + "<BR>");
</SCRIPT>
hfraser
12-09-2002, 06:33 PM
in your second posting your error is because the variable CBA is not defined
try but if you enclude you first posting code with your second posting
all works perfectly
now i should suggest you that using a form for this would be a lot more
user friendly and simplify your life a deal more
this id how it goes !
<SCRIPT>
/*
This is the initial version of my assignment script.
At the moment it's just a basic program.
*/
//Get customer details first
var CustName = prompt("Please enter the customer's name");
var CustAddy = prompt("Please enter the customer's address");
var CustSex = prompt("Please enter the customer's sex","Male");
var CustYOB = parseInt(prompt("Please enter the customer's year of birth"));
//Now get the house details
var PropAddy = prompt("Please enter the property address");
var NoBeds = parseInt(prompt("Please enter the number of bedrooms",1));
var PropType = prompt("Please enter the type of the property");
var PropPrice = parseInt(prompt("Please enter the selling price of the property"));
while ((CTB != "A") && (CTB != "B") && (CTB != "C"))
var CTB = prompt("Please enter the Council Tax band of the property");
//Calculate Council Tax
if ((PropPrice >= 200000) && (CTB == "A"))
{var CT = 0.6}
else
if ((PropPrice < 200000) && (CTB == "A"))
{var CT = 0.5}
else
if ((PropPrice >= 150000) && (CTB == "B"))
{var CT = 0.35}
else
if ((PropPrice < 150000) && (CTB == "B"))
{var CT = 0.4}
else
if ((PropPrice >= 100000) && (CTB == "C"))
{var CT = 0.25}
else
if ((PropPrice < 100000) && (CTB == "C"))
{var CT = 0.15}
else
alert("Sumthin' ain't right, foo'!");
//Calculate Property Tax
var PTBA = new Array (NoBeds)
for (var BedArea = 0; BedArea < NoBeds; BedArea ++)
{
var BedLength = parseFloat(prompt("Please enter the length of bedroom " + (BedArea + 1)));
var BedWidth = parseFloat(prompt("Please enter the width of bedroom " + (BedArea + 1)));
var BA = (BedLength*BedWidth);
PTBA [BedArea] = BA
}
if (NoBeds > 1)
{
var PropTaxBA = new Array (NoBeds - 1)
var Pos = 0
var Trans = 0
var PropTaxBigRoom = PTBA [Pos]
for (Pos = 1; Pos < NoBeds; Pos ++)
{
if (PropTaxBigRoom > PTBA [Pos])
{ PropTaxBA [(Pos - 1)] = PTBA [Pos] }
else
{ Trans = PropTaxBigRoom
PropTaxBigRoom = PTBA [Pos]
PropTaxBA [(Pos - 1)] = Trans
}
}
}
else
{var PropTaxBA = PTBA}
var CBA = 0
for (var AddBA = 0; AddBA < NoBeds; AddBA ++)
{
CBA = CBA + PropTaxBA[AddBA]
}
//Display output
document.write("<H1>" + "Calculation of Property and Council Tax:" + "</H1><BR>");
document.write("<B>" + "CUSTOMER NAME: " + "</B>" + CustName + "<BR>");
document.write("<B>" + "CUSTOMER ADDRESS: " + "</B>" + CustAddy + "<BR>");
document.write("<B>" + "CUSTOMER'S SEX: " + "</B>" + CustSex + "<BR>");
document.write("<B>" + "CUSTOMER'S YEAR OF BIRTH: " + "</B>" + CustYOB + "<BR>");
document.write("<HR>");
document.write("<B>" + "ADDRESS OF PROPERTY: " + "</B>" + PropAddy + "<BR>");
document.write("<B>" + "NUMBER OF BEDROOMS IN PROPERTY: " + "</B>" + NoBeds + "<BR>");
document.write("<B>" + "TYPE OF PROPERTY: " + "</B>" + PropType + "<BR>");
document.write("<B>" + "PROPERTY PRICE: " + "</B>" + "£" + PropPrice + "<BR>");
document.write("<HR>");
document.write("<B>" + "COUNCIL TAX BAND: " + "</B>" + CTB + "<BR>");
document.write("<B>" + "COUNCIL TAX: " + "</B>" + CT + "%" + "<BR>");
document.write("<HR>");
document.write("<B>" + "TOTAL AREA OF BEDROOMS IN PROPERTY: " + "</B>" + PropTaxBA + "<BR>" + CBA + "<BR>");
//document.write("<B>" + "PROPERTY TAX: " + "</B>" + "£" + TPT + "<BR>");
//Test Arrays
document.write("Contents of array PTBA = " + PTBA + "<BR>");
document.write("Contents of array PropTaxBA = " + PropTaxBA + "<BR>");
document.write("Contents of variable PropTaxBigRoom = " + PropTaxBigRoom + "<BR>");
</SCRIPT>
Firthy
12-10-2002, 04:23 AM
I do need to produce another version of this that does use a HTML Form, as well as that one listed.
Sceiron
12-10-2002, 12:46 PM
Based on the little bit of code originally posted, try:
CBA += PropTaxBA[AddBA] - 0;
Firthy
12-10-2002, 12:53 PM
Did you say you got it to work? Only it's still not working here.
Firthy
12-10-2002, 12:57 PM
Sceiron : It's still giving NaN.
hfraser
12-10-2002, 01:16 PM
yep ... works on all my browsers!.
nn 7
ie 5.2 mac
mozzila
chimera
mac osx
Firthy
12-10-2002, 01:21 PM
Well I've tried it in IE 6 & Netscape Communicator 4.79 under Windows XP, and both give me NaN.