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

5
  • 2
    Where are you initializing verygoodCategories1 as an array? Commented Sep 16, 2014 at 19:48
  • 1
    Where are you initializing verygoodCategories1 as an array? Should be at least a verygoodCategories1 = []; in there somewhere. Commented Sep 16, 2014 at 19:48
  • 1
    Also, you should not use for-in loops on arrays. stackoverflow.com/questions/500504/… Commented Sep 16, 2014 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 :-) Commented Sep 16, 2014 at 19:53
  • Here is a fiddle with the actual code: jsfiddle.net/zd1y6w8e Commented Sep 16, 2014 at 20:10

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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]);
    }
}

1 Comment

@Jacques Would you comment your code in the jsfiddle, where the problem is like what is the array, when it is being "undefined"
0

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

verygoodCategories1 = new Array();

2 Comments

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...
Ah yeah, i missed seeing the for loop above. The array should be created as the first statement in the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.