if it's NaN make it 0

  • Thread starter Thread starter rodchar
  • Start date Start date
R

rodchar

hey all,
ph_area_1_appraiserscore =
isNaN(ph_area_1_appraiserscore)?0:ph_area_1_appraiserscore;

is this the only way to say this?

thanks,
rodchar
 
rodchar said:
hey all,
ph_area_1_appraiserscore =
isNaN(ph_area_1_appraiserscore)?0:ph_area_1_appraiserscore;

is this the only way to say this?

Yes but you could always place in a function to keep your code tidy. I
would add a method to the Number prototype:-


Number.prototype.valueOrZero = function()
{ return isNaN(this) ? 0 : this.valueOf();}


Now you could use:-

ph_area_1_appraiserscore = ph_area_1_appraiserscore.valueOrZero();

Which would look better (only slightly because the variable name is so
long).

Of course this could be done at source since the most likely source of a NaN
is a parse. E.g.

ph_area_1_appraiserscore = parseInt(sSomeSource).valueOrZero();


BTW do you have other variables that begin with the prefix ph_area_1_ ?
Also do you have an even greater range of variables that begin with
ph_area_n_ where n is from a set of integers?

If so isn't that telling something? If not why such a mouthfull of a
variable name?
 
BTW do you have other variables that begin with the prefix ph_area_1_ ?
Also do you have an even greater range of variables that begin with
ph_area_n_ where n is from a set of integers?

If so isn't that telling something? If not why such a mouthfull of a
variable name?
ph_area_1_appraiser_score
ph_area_2_appraiser_score
ph_area_3_appraiser_score
ph_area_4_appraiser_score
ph_area_5_appraiser_score

what do you suggest?
 
i have 5 appraiser factors rated 1-5.
when the appraiser is on a new review date all scores aren't calculated yet.
so when the first rating is entered the other factor labels are blank.
and what i want to happen is a running total at the bottom.
since the other factors are blank and not a number how do i
add numbers with all the others that are NaN?
 
rodchar said:
ph_area_1_appraiser_score
ph_area_2_appraiser_score
ph_area_3_appraiser_score
ph_area_4_appraiser_score
ph_area_5_appraiser_score

what do you suggest?


At least an array of scores. However the fact that the variable has a clear
indexer in the middle variable name implies that there may be other
variables such as:-

ph_area_2_some_other_property

Which would might describe some other property of a 'ph_area object'.

So perhaps an array objects is actually called for e.g.:-

function PhArea() {}
PhArea.prototype.putAppraiserScore = function(value)
{
this.appraiserScore = parseInt(value).valueOrZero();
}

var aPhArea = new Array(5);
aPhArea.push(new PhArea())
aPhArea.push(new PhArea())
aPhArea.push(new PhArea())
aPhArea.push(new PhArea())
aPhArea.push(new PhArea())

aPhArea(0).putAppraiserScore('5')
aPhArea(1).putAppraiserScore('0')
aPhArea(2).putAppraiserScore('rubbish')
aPhArea(3).putAppraiserScore('10 green bottles') //what should happen here?
aPhArea(4).putAppraiserScore('5,000,000') // and here?

for (var i = 0; i < aPhArea.length; i++)
{
var phArea = aPhArea(i);

// code that does stuff with each phArea object.

}


When you find yourself creating many variables you should ask yourself, Is
there actually a collection of some form here? and, Do a set of variables
actually describe an object?
 
At least an array of scores. However the fact that the variable has a clear
indexer in the middle variable name implies that there may be other
variables such as:-

ph_area_2_some_other_property
That is correct, there is
ph_area_1_employee_score
ph_area_2_employee_score
ph_area_3_employee_score
ph_area_4_employee_score
ph_area_5_employee_score
 
Back
Top