Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    breaking a fundamental law of mathematics

    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));
    }
    ***alert(seeNSNnums[1]==5);
    ***alert(num==5);
    ***alert(seeNSNnums[1]==num);
    ***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

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Flint, Michigan, USA
    Posts
    627
    Thanks
    1
    Thanked 20 Times in 20 Posts
    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);

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    alert(seeNSNnums[1].toString() == num.toString());

    and should return to you in both cases "true". Note that I tested in IE5.5, perhaps this is a browser issue.

  • #4
    New Coder
    Join Date
    Jun 2002
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    No browser issue -- I dropped the "new" from my Number statement and it worked perfectly with IE6 and NN6. Thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •