0
var activePlayer = 0;

var currentScore0 = 0;

var currentScore1 = 0 ;

var player ="currentScore"+activePlayer;

This gives me a string "currentScore0" how do I get the value from the variable currentScore0

aman jha
  • 3
  • 2
  • Don't do that; have an array `currentScore = [0, 0]` and then you can just `currentScore[activePlayer]`. – melpomene Jan 07 '18 at 13:03

2 Answers2

0

You can use window["currentScore"+activePlayer] to access that variable's value but you should find a better way of storing this info so it will be easier to access it.

Here is an example of storing the info:

var players = [{
                id: 1,
                score: 0,
                active: true
              }, {
                id: 2,
                score: 0,
                active: false
              }, getPlayer: function(id){
                   return this.find(player => player.id == id);
              }];

Then you can do:

  var playerOneScore = players.getPlayer(1).score;
Titus
  • 22,031
  • 1
  • 23
  • 33
0

You could use the "eval" function.

Gerard H. Pille
  • 2,528
  • 1
  • 13
  • 17