Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Annoying Javascript prob & Variable scope

Options
  • 19-09-2002 10:48am
    #1
    Registered Users Posts: 68,317 ✭✭✭✭


    OK, having just learned Javascript, I'm practising and writing some nice simple scripts to build experience.

    I started one last night that really frustrated me....
    (I don't have the code with me and am not writing it out again)

    I'll just explain what I'll mean by 'reduced' numbers:
    Given any integer, I have a function "reduce(int)", which converts it to a single digit integer (0-9) by summing the digits together (recursively if needed). Eg., given 345, I 'reduce' it to 3 => 3 + 4 + 5 = 12 => 1 + 2 = 3.
    reduce(int) takes a number argument and returns a number argument.

    OK,

    Now what I want to do is display an i x j table, where each cell (i, j) contains the reduced value of (i*j).

    Everything goes swimmingly, two nested for-loops,eg:
    for(i=1; i < 10; i++) {
         document.write("<tr>")
         for(j=1; j < 10; j++) {
              document.write("<td>" + [b]value[/b] + "</td>")
         }
         document.write("</tr>")
    }
    
    until I try to put in the value. Putting in '(i*j)' instead of 'value' above works fine, but if I put in 'reduce(i*j)', it all goes wrong, and ends up in an endless loop.

    What I think is the problem:
    The reduce() function also has a for-loop, using i as it's iterator, so when this finished, i becomes '2' (for 2-digits nos) and so the loop cannot exit, because the guard is never breached.

    I would assume though, that in a for-loop, i is just an incremental iterator, and not an actual reference to a Number object named 'i'. Maybe I'm wrong. Any JS gurus around? :)

    (Oh, and I'm well aware that php or similar would do this much better, but I'm just writing this for practise, I don'tneed it for anything. I did write it in PHP later, just to make sure I hadn't lost my marbles ;))


Comments

  • Registered Users Posts: 3,886 ✭✭✭cgarvey


    where is 'i' declared?

    this example should work ...

    var i = 22;

    function test() {
    var i = 0;
    for( i = 0; i < 10; i++ ) {
    // do nothing
    }
    }

    ... i will never be 22 in function test (well after the first line of the function that is!)... so if reduce() declares i locally then any global i will be overwritten .. AFAIK all variables are deemed global unless specifically declared locally.


    .cg


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Originally posted by cgarvey
    AFAIK all variables are deemed global unless specifically declared locally.

    Ah. This is what I was looking for. i isn't declared anywhere, it's just the for loop iterator, and I don't need it once the for loop has finished. But does the variable reference persist, even after the loop has ended? eg.
    for(i=0; i < 10; i++) {
         //do nothing
    }
    
    document.write(i)
    

    Will this print 10(the last value of i)? If it does, then I think that's my problem solved.......

    {EDIT}
    Yes, and I just checked it, and it does print 10. Now, how do I go about setting i as an instance variable, to be thrown away, once the function has returned a value? I have a book that's heavy on examples, but light on syntax.


  • Registered Users Posts: 3,886 ✭✭✭cgarvey


    Originally posted by seamus


    Now, how do I go about setting i as an instance variable, to be thrown away, once the function has returned a value? I have a book that's heavy on examples, but light on syntax.

    Dunno really, try setting it to null .. better again why not use j for the iterator?!!

    .cg


Advertisement