0

I have a function that builds an array with push.

function one(resultsArray) {
    activity_basic_weight = 9;
        for (i in resultsArray) {
            score = 0;
            score = resultsArray[i].var_a * activity_basic_weight;
            score = Math.round(score * 100) / 100;
            if (score >= 80) 
            {
                verygoodCategories1.push({
                    score: score,
                    name: i,
                    url: resultsArray[i].url
                });
            } 
            else if (score <= 79) 
            {
                ...         
            }
        }

    two(verygoodCategories1, ...);      
}

function two(verygoodCategories1, ...) {

    alert(verygoodCategories1.length); //  = 7, correct;

    // here comes the problem:

    for (var i = 0; i < verygoodCategories1.length; i++) {
        //this throws "Error: TypeError: verygoodCategories1 is undefined"
    }
}

I am new at Javascript. Am I doing something fundamentally wrong?

This is pseudocode, but I made sure that at least the variable names are correct etc.

Any ideas?

Thanks

J

Jacques
  • 1,649
  • 2
  • 14
  • 23
  • 2
    Where are you initializing verygoodCategories1 as an array? – MVCDS Sep 16 '14 at 19:48
  • 1
    Where are you initializing `verygoodCategories1` as an array? Should be at least a `verygoodCategories1 = [];` in there somewhere. – jwatts1980 Sep 16 '14 at 19:48
  • 1
    Also, you should not use for-in loops on arrays. http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea – Chris Tavares Sep 16 '14 at 19:50
  • Can you please give use the actual code (including the invocation of `one(…)`), not pseudocode? Some `var` statements would be nice there :-) – Bergi Sep 16 '14 at 19:53
  • Here is a fiddle with the actual code: http://jsfiddle.net/zd1y6w8e/ – Jacques Sep 16 '14 at 20:10

3 Answers3

1

Assign the array parameter to a variable:

function two(verygoodCategories1, ...) {

    alert(verygoodCategories1.length); //  = 7, correct;

    var goodCategories=verygoodCategories1;
    var goodCategoriesLength=goodCategories.length;
    for (var i = 0; i < goodCategoriesLength; i++) {
        console.log(goodCategories[i]);
    }
}
Stephen P
  • 14,422
  • 2
  • 43
  • 67
Gangadhar Jannu
  • 4,136
  • 6
  • 29
  • 49
  • @Jacques Would you comment your code in the jsfiddle, where the problem is like what is the array, when it is being "undefined" – Gangadhar Jannu Sep 16 '14 at 20:16
1

Declaring variables is important. Also, as mentioned in the comments, don't use for...in. Maybe someday you will want to use that but not today.

Here is your pseudo code written so that it could actually work.

var a = [{var_a:100},{var_a:90},{var_a:81},{var_a:81},{var_a:91},{var_a:101}];
function one(resultsArray) {

    //This is critical to later using it as an array.
    var verygoodCategories1 = [];

    // you should always initialize a variable with var.  
    // You can not do it but thats bad form. You will
    // pollute the global space and people will dislike you.
    var activity_basic_weight = 9;
    for (var i = 0; i < resultsArray.length; i++) {
        var score = resultsArray[i].var_a * activity_basic_weight;
        score = Math.round(score * 100) / 100;
        if (score >= 80) 
        {
            verygoodCategories1.push({
                score: score,
                name: i,
                url: resultsArray[i].url
            });
        } 
        else if (score <= 79) 
        {
            //...
        }
    }

    two(verygoodCategories1); 
}

function two(verygoodCategories1) {

    alert(verygoodCategories1.length); //  = 7, correct;

    for (var i = 0; i < verygoodCategories1.length; i++) {
        //no more errors!
    }
}

one(a);
Jeff Wright
  • 181
  • 1
  • 8
0

Go to your function one(resultArray). just before the for, you might want to declare the array there like:

verygoodCategories1 = new Array();
Matt Rabe
  • 1,753
  • 19
  • 27
Akah
  • 1,389
  • 14
  • 19
  • 1
    This looks like bad advice. If you define `verygoodCategories1` as a new array in the location you suggest, it will wipe out the contents each time it loops... – Matt Rabe Sep 16 '14 at 19:59
  • Ah yeah, i missed seeing the for loop above. The array should be created as the first statement in the function. – Akah Sep 16 '14 at 20:04