1

I am learning Javascript for a project using online resources however i don't know how to get this function working.

var results =[[a1,a2,a3,a4,a5]];
var winner = 0;

function checkWinner (results)
{ 
  for (var i = 0; i < results.length; i++)
    if (results[0][i] > 50)
    {
      winner = results[0][i];

    }
}

Just after the function, i use: checkWinner(results);

In a HTML file i use alert to display the variable winner. But it obviously doesn't work. I realise it is a problem with my understanding of scope and global variables.

6
  • 1
    Post all of the javascript code. Commented Nov 10, 2011 at 15:16
  • can you show us place where are you calling this ? Commented Nov 10, 2011 at 15:16
  • 1
    How are you calling the function, and where are you printing the value of winner? Commented Nov 10, 2011 at 15:16
  • We don't know the values in aX variables. That might help. Commented Nov 10, 2011 at 15:17
  • Also, any particular reason why you'd use [[a1,a2,a3,a4,a5]] instead of just [a1,a2,a3,a4,a5]? Commented Nov 10, 2011 at 15:23

3 Answers 3

3

should be

var Results =[[a1,a2,a3,a4,a5]];
var winner = 0;

function checkWinner (results)
{ 
  for (var i = 0; i < results[0].length; i++)
    if (results[0][i] > 50)
    {
      winner = results[0][i];

    }
}

checkWinner(Results);

To avoid name collisions name global variables from capital case. Also in your code you call the length of the "parent" array. You need to specify the length of the "Child" array

Sign up to request clarification or add additional context in comments.

1 Comment

If you are using the global winner, why not just remove results from the argument list as well?
1

You've got to understand the concept of scope. The variables results and winner are not the same inside and outside the function.

Also, you've got to call the function and return something from it if you want to change the value of the variables outside the function (unless you use globals). This seems to be hard for novice programmers to understand, but merely defining a function doesn't do anything.

var results =[[a1,a2,a3,a4,a5]];

function checkWinner (results)
{ 
    for (var result in results[0]) 
    {
        if (result > 50)
        {
            return result;
        }
    }
}

var winner = checkWinner(results);

Note that:

  • I used a for each loop, which has a cleaner syntax.
  • I am also iterating over results[0] instead of results, since you've got a nested array for whatever reason.
  • Because your function has an argument called results, it requires you to pass the global results in spite of it being a global. Another way to do this:

var results = [[a1,a2,a3,a4,a5]];

function checkWinner()
{ 
    for (var result in results[0]) 
    {
        if (result > 50)
        {
            winner = result;
            return;
        }
    }
}

checkWinner();

However, I would recommend against using global variables this way. Here's an explanation on why global variables are bad. It's for C++, but it applies to JavaScript as well.

2 Comments

Scoping the variables is the question of the functionality. For example if you are using knockout.js then it is useful to have a viewModel as a global variable. Local variables as parameters are useful for reusability. In general it's the decision of the devloper wich to apply.
Thanks for that. I used an edited version of your code you provided there thank you. I have always had problem understanding scope and variables in Javascript. However i'm getting there. Progress has been made!
1

You're iterating over the result[0] array (the array in result[0]), but using the length of the result array.

Comments

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.