0

Here is the code I wish to update the value of "score" after a function.

function OnStart() {
  lay = app.CreateLayout("linear", "VCenter,FillXY")
  text = app.CreateText(score)
  lay.addChild(text)
  app.AddLayout(lay)
}

let score = 0;

function increment(x) {
  x++;
}

increment(score)
VLAZ
  • 26,331
  • 9
  • 49
  • 67
ax39T-Venom
  • 32
  • 1
  • 5
  • `increment(score)` will not change the `score` variable, since JavaScript is a pass-by-value language. `increment(score)` is equivalent to `increment(0)` - there is no link to the original variable that was passed in. To change a variable, you have to do it directly, for example `score++` will increment the `score` variable. – VLAZ Oct 12 '19 at 13:10
  • Possible duplicate of [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – VLAZ Oct 12 '19 at 13:12

1 Answers1

0

You need not pass any parameter to your increment function. If score is defined in the upper scope, the function will have access to it

let score = 0;
function increment(){
   score += 1;
}

console.log(score) // prints 0
increment();
console.log(score) // prints 1
Abito Prakash
  • 4,368
  • 2
  • 13
  • 26