A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: FOR loop causing NaN or Undefined

  1. #1
    Junior Member
    Join Date
    Dec 2003
    Location
    seattle
    Posts
    8

    FOR loop causing NaN or Undefined

    Here's the code:

    on (press) {
    for (var i = 0; i<=4; i++) {
    k = random(3);
    pp = new Array();
    for (var j = 0; j<=k; j++) {
    pp[j] = dofunction();
    }
    this["text"+i] = pp.join(" / ");
    trace("returned text "+i+" = "+this["text"+i]);
    _global.dofunction = function() {
    return "some text";
    };
    }
    }


    Here's the output:

    returned text 0 = undefined / undefined / undefined
    returned text 1 = some text / some text / some text
    returned text 2 = some text / some text
    returned text 3 = some text / some text / some text
    returned text 4 = some text / some text


    Why is the first return an Undefined? Sometimes (in different versions of the script) it returns NaN for the first run through but then is fine. I have tried to call "i" as a number intially : i = Number(i); and I have tried to incriment it at the start of the script : i++; but nothing seems to help.
    Thanks in advance crew!

    Aaron
    Last edited by aaronmurray; 01-17-2004 at 05:21 PM.

  2. #2
    Untitled-2.fla
    Join Date
    Jul 2002
    Posts
    391
    you have to define your function first, the way your code is set up, the first loop executes then the function is defined, so it is available for the second loop to call it

    code:
    _global.dofunction = function() {
    return "some text";
    };

    for (var i = 0; i<=4; i++) {
    k = random(3);
    pp = new Array();
    for (var j = 0; j<=k; j++) {
    pp[j] = dofunction()
    }
    this["text"+i] = pp.join(" / ");
    trace("returned text "+i+" = "+this["text"+i]);
    }


Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center