6

I wrote a for-loop that for some reason refuses to modify a global variable. Instead, it seems to create a local variable that it modifies temporarily. A condensed version of my code is as follows.

var clubsArray = [obj, obj, obj];

//each obj contains a property of "goalsFor" which holds an integer

var madridTotalGoals = 0;
var barcaTotalGoals = 0;

function findTotalGoals(clubsArray, totalGoals) {

    for(var i = 0; i < clubsArray.length; i++) {

        totalGoals += clubsArray[i].goalsFor;
    }
}

findTotalGoals(clubsArray, barcaTotalGoals);

// this loops properly and does the math, but it never changes the value of barcaTotalGoals

In the full code there are numerous arrays that hold "club" objects; each contain a property key "goalsFor", which hold an integer as a value. There are also numerous "totalGoals" variables (two are specified here) that have been declared globally.

Does anyone know why the global variable (e.g. barcaTotalGoals) is not being modified when passed through this function? When I console log each step of this loop, the math is taking place but the result is not being stored. I apologize if this has been asked before but I've searched thoroughly.

  • 1
    JavaScript is pass by value. `totalGoals` gets the value of `barcaTotalGoals` but doesn't modify it. Modification to `totalGoals` doesn't change `barcaTotalGoals`. – Andrew Li Oct 23 '16 at 04:49

2 Answers2

3

The variable you are trying to pass, is passed by value and not reference. So it wont affect the original variable

You can assign the value once the for loop is finished

function findTotalGoals(clubsArray, totalGoals) {

    for(var i = 0; i < clubsArray.length; i++) {

       totalGoals += clubsArray[i].goalsFor;
    }
    barcaTotalGoals = totalGoals;
}
Abhinav
  • 8,028
  • 12
  • 48
  • 89
  • If there is a duplicate, please close vote as such. – Andrew Li Oct 23 '16 at 04:50
  • Thank you for the hasty response, you'll have to excuse my ignorance here, but if I want to use the function to modify multiple variables (say, 12) how would I go about doing that without having to repeat the function 12 times? –  Oct 23 '16 at 04:56
  • @AndrewLi That link will definitely help OP understand the difference between Passing by Value and Reference but chances are OP would not have even known that this ques depends on this concept and there is no need to be very tough to any new comers on stackoverflow, thats just my opinion – Abhinav Oct 23 '16 at 04:58
  • The link isn't being harsh on a new user, heck it's pointing them in the right direction. The duplicate link isn't saying a question is bad or invalid, just saying that it's been asked before. No matter how experienced the user is, a duplicate is a duplicate – Andrew Li Oct 23 '16 at 05:01
  • @roz0n Can you please be a bit elaborate on what you are asking? – Abhinav Oct 23 '16 at 05:02
  • @Abhinav How do I use this function to change both the barca and madrid variables globally with minimal repetition? I'm sorry if there's something obvious I am missing here; I am very new to programming. I apologize for the dupe, I am definitely looking into the linked article. –  Oct 23 '16 at 05:16
  • @roz0n You could use some local variable inside the function. Then based on some conditions using `if` and `else`, you could update the values of local variable and then assign the local variables to the global variables accordingly – Abhinav Oct 23 '16 at 05:30
1

You are passing by value instead of by reference... Instead, you could try like this:

clubsArray = [obj, obj, obj];

var totalGoals = {
    madrid: 0,
    barca: 0
}

function goalsByCountry(clubsArray, totalGoalsClub) {

    for(var i = 0; i < clubsArray.length; i++) {

        totalGoals[totalGoalsClub] += clubsArray[i].goalsFor;

    }

}

goalsByTeam(clubsArray, 'barca');
Thejaka Maldeniya
  • 1,076
  • 1
  • 10
  • 20