PDA

View Full Version : breaking a fundamental law of mathematics

dwbh
07-11-2002, 03:10 PM
Hi,

Here's a script I'm working on that's giving me fits:

====================================

function removeRow(num) {
var frm = document.forms[0];
var num = new Number(num);
var yearsNum = new Number(frm.NumYears.value);
var divNames = buildArray("DIV", /d\d\d\da/);
var fromLen = divNames.length;
writeFirstValues(fromLen);
var seeNSNarray = getVisibleNSNs();
var seeNSNnums = new Array();
for(var x=0; x < (seeNSNarray.length); x++) {
seeNSNnums[x] = new Number(seeNSNarray[x].id.slice(3));
}
***for(var y=0; y < (seeNSNnums.length); y++) {
***if(seeNSNnums[y] == num) {var valid=true; alert(true);}
***}
***if(!valid) {alert("Please enter the number of a row containing an NSN."); return false;}
frm.NSN.value=(frm.NSN.value-1);
removed = NSN_value.splice((num-1), yearsNum);
removed = FY.splice((num-1), yearsNum);
removed = TotalQty.splice((num-1), yearsNum);
removed = MinQty.splice((num-1), yearsNum);
removed = MaxQty.splice((num-1), yearsNum);
removed = Centra.splice((num-1), yearsNum);
removed = CgCode.splice((num-1), yearsNum);
removed = RevLetter.splice((num-1), yearsNum);
removed = AMRC.splice((num-1), yearsNum);
removed = TobyBid.splice((num-1), yearsNum);
writeTable();
};

===========================

The six lines where I've added *** are the problem lines here. Notice that I have three alert functions, testing whether a variable is equal to five, then whether a second variable is also equal to five, then finally whether the first variable and second variable are equal to each other. I then set a flag variable if the two variables are equal that I use to validate the function.

When I pass five as my function parameter (num), the alert messages come back "true", "true", and "false". How can this be!? Both variable should be the same variable type (number), and I can't see anything else that would affect this. I am completely bamboozled...help!

Sam

jalarie
07-11-2002, 04:31 PM
I wonder if things are getting confused with the double definition of "num" as both a function parameter and as an internal variable:

&nbsp;&nbsp;var num = new Number(num);

mordred
07-11-2002, 04:39 PM
That's already close - drop the "new" keyword when you convert num to a number. Simply do

num = Number(num);

Curiously enough, in your special case it seems that the equality operator tests for equality in objects, not values. seeNSNnums[1] refers to a different object than num does.
I have assumed that during a simple equality testing statement both operands were called upon their toString() method, but obviously, that isn't the case. It works also with