Skip to main content
russb64585541
Participating Frequently
September 21, 2020
Question

NaN

  • September 21, 2020
  • 3 replies
  • 418 views

I'm trying to create an equation based on some fields in my form. I don't see where the error is, but the console is telling me my output is not a number. Any help would be appreciated.

 

var house = this.getField("SqFt");
var term = 120;
var margin = .0275;
var wh = 0;
var heat = 0;

if(house.value<="1500")house=1500;

if(this.getField("WaterHeater").value=="Electric Tank") wh=.03;
else
if(this.getField("WaterHeater").value=="Electric Tankless") wh=.05;

if(this.getField("Heat").value=="Combo") heat=.05;
else
if(this.getField("Heat").value=="Electric") heat=.10;

event.value = house.value*(.65 + wh + heat)*term.value*margin.value
NaN

This topic has been closed for replies.

3 replies

russb64585541
Participating Frequently
September 22, 2020

Why do you use the .value for the house variable, but not for the others? I suppose when I put the quotes around the 1500 I changed it from a number to a different type of value? I appreciate all the help with this.

Legend
September 22, 2020

You need to use .value when the object (variable) comes from a form field. The form field has many properties (name, size, colour...) and you need the specific property called value. 

You must not use .value when the object (variable) is a simple number. 

It's a pain to keep this straight, and it's the sort of mistake I often make, but keep it straight we must. 

Inspiring
September 22, 2020

Here's what I modify the script to:

 

// Get the value of the SqFt field, as a number
var house = +getField("SqFt").value;

if (house <= 1500) {
    house = 1500;
}

var term = 120;
var margin = 0.0275;
var wh = 0;
var heat = 0;

if (getField("WaterHeater").value == "Electric Tank") {
    wh = 0.03;
} else if (getField("WaterHeater").value == "Electric Tankless") {
    wh = 0.05;
}

if (getField("Heat").value == "Combo") {
    heat = 0.05;
} else if (getField("Heat").value == "Electric") {
    heat = 0.10;
}

// Set this field's value
event.value = house * (0.65 + wh + heat) * term * margin;

 

 

 

 

Also, this line:

 

if(house.value<="1500")house=1500;

 

Should be:

 

if (house.value <= 1500) house = 1500;

Inspiring
September 22, 2020

Please ignore those last four lines of my previous response. They were accidentally included from my initial attempt.

ls_rbls
Community Expert
Community Expert
September 22, 2020

In your last line you phrased your variables "term" and "margin" as  term.value and margin.value

 

Change that line to :

 

event.value = house.value*(.65 + wh + heat)*term*margin;