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.

user991735
  • 684
  • 1
  • 7
  • 15

3 Answers3

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

Oybek
  • 7,016
  • 5
  • 29
  • 49
1

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

nogola
  • 214
  • 4
  • 12
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.

Community
  • 1
  • 1
NullUserException
  • 83,810
  • 28
  • 209
  • 234
  • 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. – Oybek Nov 10 '11 at 15:47
  • 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! – user991735 Nov 10 '11 at 16:29