2

I have looked online about changing variables in an function but still doesn't work.

I'm trying to change the global variable in a function. An object with numbers are multiplied by each other. The function will be use several time with different global variables. so don't want to use there names directly.

HERE IS THE CODE

var homeTeamOppw = {"last_gp": 0.2, "2nd_gp": 0.2, "3rd_gp": 0.8, "4th_gp": 0.4, "5th_gp": 0.8};
ans = 0;

function timesArrayitems(teamOpp, array){
  var num = 1;

  for(item in array){
    num = num * array[item];
  }

  teamOpp = num;
}

timesArrayitems(ans, homeTeamOppW);
console.log(ans);

The ans come out as undefine. Can someone explain please and thanks

Evan Davis
  • 35,493
  • 6
  • 50
  • 57
user3210416
  • 794
  • 1
  • 10
  • 20
  • 1
    http://stackoverflow.com/questions/13104494/does-javascript-pass-by-reference – mplungjan Jul 15 '14 at 20:07
  • 1
    Your not actually updating `ans` in `timesArrayitems()`, you're passing it in as a reference, where it's being used as a `local var teamOpp` scoped inside the `function`. – Foreign Object Jul 15 '14 at 20:08

5 Answers5

1

Your function only changes the local copy, teamOpp. You should return a value and set ans to that.

var homeTeamOppW = {"last_gp": 0.2, "2nd_gp": 0.2, "3rd_gp": 0.8, "4th_gp": 0.4, "5th_gp": 0.8};
ans = 0;

function timesArrayitems(array){ // note only one param
    var num = 1;

    for(item in array){

      num = num * array[item];
    }

    return num; // note return value

}

 ans = timesArrayitems(homeTeamOppW);
 console.log(ans);
Evan Davis
  • 35,493
  • 6
  • 50
  • 57
1

If you need to play with global scope and want to pass ans, ans need to be an object instead of just a variable as when you call a function it will be passed by value and if object is passed that is then passed by reference.

Code Snippet:

var homeTeamOppw = {"last_gp": 0.2, "2nd_gp": 0.2, "3rd_gp": 0.8, "4th_gp": 0.4, "5th_gp": 0.8};
ans = {val:0};

function timesArrayitems(teamOpp, array){

var num = 1;

for(item in array){

    num = num * array[item];
}

    teamOpp.val = num;

}

 timesArrayitems(ans, homeTeamOppw);
 console.log(ans);

Fiddle

V31
  • 7,626
  • 3
  • 26
  • 44
  • While this works, it's a weird way to solve this problem IMO. – Evan Davis Jul 15 '14 at 20:12
  • I wanted to explain why his ans variable is always zero and not getting updated in the outer scope – V31 Jul 15 '14 at 20:15
  • Right; your explanation of the problem is correct, I just think your _solution_ is weird, using an object instead of returning a value. It seems like an overly complicated fix. – Evan Davis Jul 15 '14 at 20:18
1

I think you mean something like this:

var homeTeamOppw = {"last_gp": 0.2, "2nd_gp": 0.2, "3rd_gp": 0.8, "4th_gp": 0.4, "5th_gp": 0.8};
ans = 0;

function timesArrayitems(array){
  var num = 1.0;

  for(item in array){
    num = num * array[item];
  }
  return num;
}

ans = timesArrayitems(homeTeamOppw);
console.log(ans);

ans returns 0.01024000004 (don't know if that is the expected output).

Biketire
  • 2,019
  • 1
  • 23
  • 41
0

Well, you've got a few things going on here:

First, when I run your code I get a reference error because homeTeamOppW is not defined. You have a typo.

Second you never actually update ans; it gets passed by value. So it will never be changed from 0.

That being said, when I run your code (changing homeTeamOppW to homeTeamoppw) it prints 0 which is what I would expect.

If you change the function to return a value as @Mathletics shows and fix the typo, I think it does what you want. Alternatively, you can just directly reassign ans at the end of the function.

Mike Bell
  • 1,356
  • 11
  • 9
0

The function will have its own copy (local to the function) and will not actually modify the content of your global var ans. Instead of passing ans as a parameter, you can try to assign whatever value the function is returning inside the function body.

Thanks

Mahder
  • 409
  • 3
  • 9