August 5th, 2009, 02:57 PM
#1
Total Floor Area is showing a NaN
I have a form which is used to calculate residential Floor Area Ratio (FAR). The form is structured into seven parts as follows:
Part A: Maximum FAR and Floor Area:
Part B: Gross Floor Area of the main floors of the main house:
Part C: Gross Floor Area of the basement or cellar:
Part D: Gross Floor Area of the attic:
Part E. Gross Floor Area of all accessory structures except detached garages: (including cabanas, guest houses, caretaker's cottages, pool houses, sheds, barns, or other structures except a detached garage)
Part F. Gross Floor Area of the garage: (not including basement garages)
Part G: Total Floor Area:
The Javascript involved in the calculations is as follows:
Code:
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
function checkAllInputFields(){
IsValidEntry(entireForm.A1.value)
IsValidEntry(entireForm.A5.value)
IsValidEntry(entireForm.A6.value)
IsValidEntry(entireForm.A8.value)
IsValidEntry(entireForm.A9.value)
IsValidEntry(entireForm.B1.value)
IsValidEntry(entireForm.B3.value)
IsValidEntry(entireForm.B4.value)
IsValidEntry(entireForm.B5.value)
IsValidEntry(entireForm.C1.value)
IsValidEntry(entireForm.C2.value)
IsValidEntry(entireForm.C3.value)
IsValidEntry(entireForm.D3.value)
IsValidEntry(entireForm.D4.value)
IsValidEntry(entireForm.D6.value)
IsValidEntry(entireForm.D7.value)
IsValidEntry(entireForm.D9.value)
IsValidEntry(entireForm.D10.value)
IsValidEntry(entireForm.D12.value)
IsValidEntry(entireForm.D13.value)
IsValidEntry(entireForm.D14.value)
IsValidEntry(entireForm.D15.value)
IsValidEntry(entireForm.E1.value)
IsValidEntry(entireForm.F2.value)
IsValidEntry(entireForm.F3.value)
IsValidEntry(entireForm.F4.value)
IsValidEntry(entireForm.F5.value)
IsValidEntry(entireForm.F8.value)
IsValidEntry(entireForm.F9.value)
IsValidEntry(entireForm.F11.value)
IsValidEntry(entireForm.F12.value)
IsValidEntry(entireForm.F14.value)
IsValidEntry(entireForm.F15.value)
IsValidEntry(entireForm.F17.value)
IsValidEntry(entireForm.F18.value)
IsValidEntry(entireForm.F19.value)
IsValidEntry(entireForm.F20.value)
}
function IsValidEntry(entry){
if(entry == "N/A to this property" || entry == "" || IsNumeric(entry)){
//do nothing
}
else{
alert(entry + " is not a number. Numbers must only contain digits and decimal points")
}
}
function IsNumeric(sText){
var ValidChars = "0123456789.";
var IsNumber=true;
var Char;
for (i = 0; i < sText.length && IsNumber == true; i++) {
Char = sText.charAt(i);
if (ValidChars.indexOf(Char) == -1) {
IsNumber = false;
}
}
return IsNumber;
}
//function which returns given number to 2 decimal places
function roundTwo(x) {
return (Math.round(x*Math.pow(10,2)))/Math.pow(10,2)
}
// function which returns given number to 4 decimal places
function roundFour(x) {
return (Math.round(x*Math.pow(10,4)))/Math.pow(10,4)
}
// function which calls calculation functions for all sections
// this function is called whenever anything is changed on the form
function calculateAll(){
checkAllInputFields()
doZoneChange()
doPartA()
doPartB()
doPartC()
doPartD()
doPartE()
doPartF()
doPartG()
}
//funtion that changes form according to zoning district
function doZoneChange(){
if (entireForm.zone.value == "AA1"){
entireForm.A5.value = "N/A to this property"
entireForm.A5.disabled = true
entireForm.A6.value = "N/A to this property"
entireForm.A6.disabled = true
entireForm.A7.value = "---------------------"
entireForm.A8.value = "N/A to this property"
entireForm.A8.disabled = true
entireForm.A9.value = "N/A to this property"
entireForm.A9.disabled = true
entireForm.A10.value = "---------------------"
entireForm.A11.value = "---------------------"
entireForm.A12.value = "---------------------"
entireForm.A13.value = "---------------------"
entireForm.A14.value = "N/A to this property"
}
else{
if (entireForm.A6.value == "N/A to this property"){
entireForm.A5.value = ""
entireForm.A5.disabled = false
entireForm.A6.value = ""
entireForm.A6.disabled = false
entireForm.A7.value = ""
entireForm.A8.value = ""
entireForm.A8.disabled = false
entireForm.A9.value = ""
entireForm.A9.disabled = false
entireForm.A10.value = ""
entireForm.A11.value = ""
entireForm.A12.value = ""
entireForm.A13.value = ""
entireForm.A14.value = ""
if (entireForm.zone.value == "A1"){
entireForm.A13.value = "1200"
}
else if (entireForm.zone.value == "A4" || entireForm.zone.value == "A5"){
entireForm.A13.value = "400"
}
else{
entireForm.A13.value = "700"
}
}
else{
if (entireForm.zone.value == "A1"){
entireForm.A13.value = "1200"
}
else if (entireForm.zone.value == "A4" || entireForm.zone.value == "A5"){
entireForm.A13.value = "400"
}
else{
entireForm.A13.value = "700"
}
}
}
}
function doPartA(){
// Calculates A2
var lotArea = entireForm.A1.value
if(lotArea < 5000){
entireForm.A2.value = .43
}
else if(lotArea < 10000){
entireForm.A2.value = (.43-(((lotArea-5000)/1000)*.016))
}
else if(lotArea < 15000){
entireForm.A2.value = (.35-(((lotArea-10000)/1000)*.012))
}
else if(lotArea < 30000){
entireForm.A2.value = (.29-(((lotArea-15000)/1000)*.006))
}
else if(lotArea < 35000){
entireForm.A2.value = (.20-(((lotArea-30000)/1000)*.0045))
}
else if(lotArea < 40000){
entireForm.A2.value = (.1775-(((lotArea-35000)/1000)*.003))
}
else if(lotArea < 45000){
entireForm.A2.value = (.1625-(((lotArea-40000)/1000)*.002))
}
else if(lotArea < 50000){
entireForm.A2.value = (.1525-(((lotArea-45000)/1000)*.0015))
}
else if(lotArea < 76231){
entireForm.A2.value = .145
}
else {
entireForm.A2.value = 0
}
entireForm.A2.value = roundFour(entireForm.A2.value)
// Calculates A4
if(lotArea < 76231){
var maxFar = entireForm.A2.value
entireForm.A4.value = (roundTwo(lotArea * maxFar))
}
else{
entireForm.A4.value = 15000
}
// Calculates A7 & A10
var RRightSetBack = entireForm.A5.value
var PRightSetBack = entireForm.A6.value
var RLeftSetBack = entireForm.A8.value
var PLeftSetBack = entireForm.A9.value
entireForm.A7.value = (PRightSetBack - RRightSetBack)
if(entireForm.A7.value <= 0){
entireForm.A7.value = 0
}
entireForm.A10.value = roundFour(PLeftSetBack - RLeftSetBack)
if(entireForm.A10.value <= 0){
entireForm.A10.value = 0
}
leftResult = entireForm.A10.value
rightResult = entireForm.A7.value
// Calculates A11
entireForm.A11.value = roundFour(Number(leftResult) + Number(rightResult))
// Calculates A12
entireForm.A12.value = roundFour(entireForm.A11.value * 100)
// Calculates A14
if(entireForm.zone.value == "AA1"){
entireForm.A14.value = "0"
}
else{
var formA13 = entireForm.A13.value
var formA12 = entireForm.A12.value
if(Number(formA13) < Number(formA12)){
entireForm.A14.value = formA13
}
else{
entireForm.A14.value = formA12
}
}
//-------------------------------------
// Calculates A15
//--------------------------------------
entireForm.A15.value = (Number(entireForm.A14.value) + Number(entireForm.A4.value))
}
function doPartB(){
//-------------------------------------
// Calculates B2 & B6
//--------------------------------------
var formB2 = (entireForm.B1.value * 2)
var formB3 = entireForm.B3.value
var formB4 = entireForm.B4.value
var formB5 = entireForm.B5.value
entireForm.B2.value = formB2
entireForm.B6.value = roundTwo(Number(formB2) + Number(formB3) + Number(formB4) + Number(formB5))
}
var firstTimeThrough = 0
function doPartC(){
//-------------------------------------
// Calculates C4
//--------------------------------------
var tallestPoint = entireForm.C1.value
if( !(entireForm.C1.value == "") ){
firstTimeThrough = 1
}
if ( (tallestPoint < 3) && (firstTimeThrough == 1) ){
entireForm.C2.value = "N/A to this property"
entireForm.C2.disabled = true
entireForm.C3.value = "N/A to this property"
entireForm.C3.disabled = true
entireForm.C4.value = "0"
}
else{
if (entireForm.C2.value == "N/A to this property"){
entireForm.C2.value = ""
entireForm.C2.disabled = false
entireForm.C3.value = ""
entireForm.C3.disabled = false
entireForm.C4.value = ""
}
else{
entireForm.C4.value = roundTwo(entireForm.C3.value * entireForm.C2.value)
}
}
}
function doPartD(){
if(entireForm.D1.value == "No" || entireForm.D2.value == "No"){
entireForm.D3.value = "N/A to this property"
entireForm.D3.disabled = true
entireForm.D4.value = "N/A to this property"
entireForm.D4.disabled = true
entireForm.D5.value = "---------------------"
entireForm.D6.value = "N/A to this property"
entireForm.D6.disabled = true
entireForm.D7.value = "N/A to this property"
entireForm.D7.disabled = true
entireForm.D8.value = "---------------------"
entireForm.D9.value = "N/A to this property"
entireForm.D9.disabled = true
entireForm.D10.value = "N/A to this property"
entireForm.D10.disabled = true
entireForm.D11.value = "---------------------"
entireForm.D12.value = "N/A to this property"
entireForm.D12.disabled = true
entireForm.D13.value = "N/A to this property"
entireForm.D13.disabled = true
entireForm.D14.value = "N/A to this property"
entireForm.D14.disabled = true
entireForm.D15.value = "N/A to this property"
entireForm.D15.disabled = true
entireForm.D16.value = "0"
}
else if(entireForm.D3.value == "N/A to this property"){
entireForm.D3.value = ""
entireForm.D3.disabled = false
entireForm.D4.value = ""
entireForm.D4.disabled = false
entireForm.D5.value = ""
entireForm.D6.value = ""
entireForm.D6.disabled = false
entireForm.D7.value = ""
entireForm.D7.disabled = false
entireForm.D8.value = ""
entireForm.D9.value = ""
entireForm.D9.disabled = false
entireForm.D10.value = ""
entireForm.D10.disabled = false
entireForm.D11.value = ""
entireForm.D12.value = ""
entireForm.D12.disabled = false
entireForm.D13.value = ""
entireForm.D13.disabled = false
entireForm.D14.value = ""
entireForm.D14.disabled = false
entireForm.D15.value = ""
entireForm.D15.disabled = false
entireForm.D16.value = ""
}
else{
//-------------------------------------
// Calculates D5
//--------------------------------------
if(entireForm.D3.value == "0"){
entireForm.D4.value = "0"
entireForm.D5.value = "0"
}
else{
entireForm.D5.value = roundFour((entireForm.D4.value / entireForm.D3.value)*100)
}
//-------------------------------------
// Calculates D8
//--------------------------------------
if(entireForm.D6.value == "0"){
entireForm.D7.value = "0"
entireForm.D8.value = "0"
}
else{
entireForm.D8.value = roundFour((entireForm.D7.value / entireForm.D6.value)*100)
}
//-------------------------------------
// Calculates D11
//--------------------------------------
if(entireForm.D9.value == "0"){
entireForm.D10.value = "0"
entireForm.D11.value = "0"
}
else{
entireForm.D11.value = roundFour((entireForm.D10.value / entireForm.D9.value)*100)
}
if(entireForm.D12.value == "0"){
entireForm.D13.value = "0"
}
if(entireForm.D4.value == ""){
entireForm.D5.value = "NMI"
}
if(entireForm.D7.value == ""){
entireForm.D8.value = "NMI"
}
if(entireForm.D10.value == ""){
entireForm.D11.value = "NMI"
}
//-------------------------------------
// Calculates D16
//--------------------------------------
if(entireForm.D5.value < 30 && entireForm.D8.value < 30 && entireForm.D11.value < 30 && entireForm.D12.value >= 5 && entireForm.D13.value >= 5){
entireForm.D14.value = "N/A to this property"
entireForm.D14.disabled = true
entireForm.D15.value = "N/A to this property"
entireForm.D15.disabled = true
entireForm.D16.value = "N/A to this property"
}
else if (entireForm.D14.value == "N/A to this property"){
entireForm.D14.value = ""
entireForm.D14.disabled = false
entireForm.D15.value = ""
entireForm.D15.disabled = false
entireForm.D16.value = ""
}
if (!(entireForm.D14.value == "") && !(entireForm.D15.value == "") && !(entireForm.D15.value == "N/A to this property") && !(entireForm.D15.value == "N/A to this property")){
entireForm.D16.value = (entireForm.D14.value - entireForm.D15.value)
}
}
}
function doPartE(){
//-------------------------------------
// Calculates E2
//--------------------------------------
entireForm.E2.value = entireForm.E1.value
}
function doPartF(){
if(entireForm.F0.value == "No"){
entireForm.F3.value = "0"
entireForm.F3.disabled = true
entireForm.F4.value = "0"
entireForm.F4.disabled = true
entireForm.F5.value = "0"
entireForm.F5.disabled = true
entireForm.F6.value = "No"
entireForm.F6.disabled = true
entireForm.F7.disabled = true
}
else if(entireForm.F0.value == "Yes" && entireForm.F3.value == "0"){
entireForm.F3.value = ""
entireForm.F3.disabled = false
entireForm.F4.value = ""
entireForm.F4.disabled = false
entireForm.F5.value = ""
entireForm.F5.disabled = false
entireForm.F6.value = "Yes"
entireForm.F6.disabled = false
entireForm.F7.disabled = false
}
if(entireForm.F1.value == "No"){
entireForm.F2.value = "0"
entireForm.F2.disabled = true
}
else if(entireForm.F1.value == "Yes" && entireForm.F2.value == "0"){
entireForm.F2.value = ""
entireForm.F2.disabled = false
}
if(entireForm.F6.value == "No" || entireForm.F7.value == "No"){
entireForm.F8.value = "N/A to this property"
entireForm.F8.disabled = true
entireForm.F9.value = "N/A to this property"
entireForm.F9.disabled = true
entireForm.F10.value = "---------------------"
entireForm.F11.value = "N/A to this property"
entireForm.F11.disabled = true
entireForm.F12.value = "N/A to this property"
entireForm.F12.disabled = true
entireForm.F13.value = "---------------------"
entireForm.F14.value = "N/A to this property"
entireForm.F14.disabled = true
entireForm.F15.value = "N/A to this property"
entireForm.F15.disabled = true
entireForm.F16.value = "---------------------"
entireForm.F17.value = "N/A to this property"
entireForm.F17.disabled = true
entireForm.F18.value = "N/A to this property"
entireForm.F18.disabled = true
entireForm.F19.value = "N/A to this property"
entireForm.F19.disabled = true
entireForm.F20.value = "N/A to this property"
entireForm.F20.disabled = true
entireForm.F21.value = "0"
}
else if(entireForm.F8.value == "N/A to this property"){
entireForm.F8.value = ""
entireForm.F8.disabled = false
entireForm.F9.value = ""
entireForm.F9.disabled = false
entireForm.F10.value = ""
entireForm.F11.value = ""
entireForm.F11.disabled = false
entireForm.F12.value = ""
entireForm.F12.disabled = false
entireForm.F13.value = ""
entireForm.F14.value = ""
entireForm.F14.disabled = false
entireForm.F15.value = ""
entireForm.F15.disabled = false
entireForm.F16.value = ""
entireForm.F17.value = ""
entireForm.F17.disabled = false
entireForm.F18.value = ""
entireForm.F18.disabled = false
entireForm.F19.value = ""
entireForm.F19.disabled = false
entireForm.F20.value = ""
entireForm.F20.disabled = false
entireForm.F21.value = "0"
}
else{
//-------------------------------------
// Calculates F10
//--------------------------------------
if(entireForm.F8.value == "0"){
entireForm.F9.value = "0"
entireForm.F10.value = "0"
}
else{
entireForm.F10.value = roundFour((entireForm.F9.value / entireForm.F8.value)*100)
}
//-------------------------------------
// Calculates F13
//--------------------------------------
if(entireForm.F11.value == "0"){
entireForm.F12.value = "0"
entireForm.F13.value = "0"
}
else{
entireForm.F13.value = roundFour((entireForm.F12.value / entireForm.F11.value)*100)
}
//-------------------------------------
// Calculates F16
//--------------------------------------
if(entireForm.F14.value == "0"){
entireForm.F15.value = "0"
entireForm.F16.value = "0"
}
else{
entireForm.F16.value = roundFour((entireForm.F15.value / entireForm.F14.value)*100)
}
if(entireForm.F17.value == "0"){
entireForm.F18.value = "0"
}
if(entireForm.F9.value == ""){
entireForm.F10.value = "NMI"
}
if(entireForm.F12.value == ""){
entireForm.F13.value = "NMI"
}
if(entireForm.F15.value == ""){
entireForm.F16.value = "NMI"
}
//-------------------------------------
// Calculates F21
//--------------------------------------
if(entireForm.F10.value < 30 && entireForm.F13.value < 30 && entireForm.F16.value < 30 && entireForm.F17.value >= 5 && entireForm.F18.value >= 5){
entireForm.F19.value = "N/A to this property"
entireForm.F19.disabled = true
entireForm.F20.value = "N/A to this property"
entireForm.F20.disabled = true
entireForm.F21.value = "N/A to this property"
}
else if (entireForm.F19.value == "N/A to this property"){
entireForm.F19.value = ""
entireForm.F19.disabled = false
entireForm.F20.value = ""
entireForm.F20.disabled = false
entireForm.F21.value = ""
}
if (!(entireForm.F19.value == "") && !(entireForm.F20.value == "") && !(entireForm.F20.value == "N/A to this property") && !(entireForm.F20.value == "N/A to this property")){
entireForm.F21.value = roundTwo(entireForm.F19.value - entireForm.F20.value)
}
}
//-------------------------------------
// Calculates F23
//--------------------------------------
entireForm.F22.value = entireForm.A1.value
if(entireForm.F22.value <= 10000){
entireForm.F23.value = 250
}
else{
entireForm.F23.value = 400
}
//-------------------------------------
// Calculates F24 & F25
//--------------------------------------
entireForm.F24.value = roundFour(Number(entireForm.F2.value) + Number(entireForm.F3.value))
entireForm.F25.value = roundFour(entireForm.F24.value - entireForm.F23.value)
//-------------------------------------
// Calculates F26
//--------------------------------------
if(entireForm.F25.value < 0){
entireForm.F25.value = 0
}
entireForm.F26.value = roundTwo(Number(entireForm.F4.value) + Number(entireForm.F5.value) + Number(entireForm.F21.value) + Number(entireForm.F25.value))
}
function doPartG(){
//-------------------------------------
// Calculates G1 & G2
//--------------------------------------
entireForm.G1.value = roundTwo(Number(entireForm.B6.value) + Number(entireForm.C4.value) + Number(entireForm.D16.value) + Number(entireForm.E2.value) + Number(entireForm.F26.value))
entireForm.G2.value = entireForm.A15.value
//-------------------------------------
// Calculates G4 & G5
//--------------------------------------
if (entireForm.G1.value > entireForm.G2.value){
entireForm.G3.value = "Yes"
entireForm.G4.value = roundTwo(entireForm.G1.value - entireForm.G2.value)
}
else{
entireForm.G3.value = "No"
entireForm.G4.value = "Not exceeded"
}
}
</SCRIPT>
I have attached the html file as a .txt file for reference, and I have attached a .txt document including all the values I used for testing.
Why is it showing NaN values? Any help would be greatly appreciated.
Thank You.
Attached Files
August 5th, 2009, 06:21 PM
#2
Re: Total Floor Area is showing a NaN
Observations:
1. I wouldn't recommend accessing the form directly via its name, not all browsers would support this. Use something like: var myForm = document.entireForm; and then use myForm instead.
2. In place of running a skyscraper of IsValidEntry()'s just change it so it could receive an array as a parameter, or run a loop.
3. I'd recommend checking the result of each validation instead of running the whole structure even if the very first value fails (make all validation methods return a boolean for ex.).
Code:
function checkAllInputFields(){
var result = false ;
var fields = new Array(entireForm.A1.value,
entireForm.A5.value,
entireForm.A6.value); //...
for (i=0; i<fields.length; i++){
if (!(result = IsValidEntry(fields[i]))){break ;}
}
return result;
}
function isValidEntry(entry){
var result = false ;
if (entry == "N/A to this property" || entry == "" || IsNumeric(entry)){
result = true ;
}else {
alert(entry + " is not a number." );
}
return result;
}
4. Javascript already has its own number validation function:
Code:
if (isNaN(myString)){
//this is not a number
}else {
//this is a number
}
5. Use semicolon at the end of each expression: sometimes it's really difficult to read/edit even your own code when all lines look the same.
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood (Arrays .asList ("Java" ,"PHP" ,"C++" ,"bash" ,"Assembler" ,"XML" ,"XHTML" ,"CSS" ,"JS" ,"PL/SQL" ));
USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!
August 6th, 2009, 09:36 AM
#3
Re: Total Floor Area is showing a NaN
Thanks for the help and insight, I really appreciate it. I really like the
function isValidEntry. So how do you think I should modify this to get it to work correctly. Can I use the isNan function to read the D16 value and if the value is (N/A to this property) then exclude it from calculating G1 and if it is a number to use it in calculating G1.
What do you think?
Regards,
Nick
August 6th, 2009, 11:14 AM
#4
Re: Total Floor Area is showing a NaN
I really like the function isValidEntry. So how do you think I should modify this to get it to work correctly.
It's already changed in my first post.
Can I use the isNan function to read the D16 value and if the value is (N/A to this property) then exclude it from calculating G1 and if it is a number to use it in calculating G1.
A strange question... Yes, just add an if statement to the point where you calculate G1 checking D16 value...
About isNaN() : I've just mentioned this method because I saw you used IsNumeric() for the same task.
Last edited by Xeel; August 6th, 2009 at 11:17 AM .
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood (Arrays .asList ("Java" ,"PHP" ,"C++" ,"bash" ,"Assembler" ,"XML" ,"XHTML" ,"CSS" ,"JS" ,"PL/SQL" ));
USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!
August 6th, 2009, 11:45 AM
#5
Re: Total Floor Area is showing a NaN
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width