2

I'm not clear on the correct syntax to do the following.

  • I have tried eval() I have tried [] and {[]} as well..

I start with this:

var player1InitStart = 0;
var player2InitStart = 0;
var player3InitStart = 0;
var player4InitStart = 0;

var playerID =  event.target.id; // will return, player1, 2, 3, 4

To be used in a check like so:

if(eval(playerID + "InitString") == 0){
    //do something, first time starting video
    eval(playerID + "InitString") = 1;
    alert('First Time Playing Video' + eval(playerID + "InitString"));
}

I am trying to use the playerID value together with the string: "InitStart"..

so I can then update the target xxInitStart variable above.

How can I concatenate the var playerID with the string InitStart so I can now target one of the playerXInitSTart variables to update it?

Update: answer/solution that worked for me-

  • no clue why the $ character is being used? (reminds me of PHP)
  • no clue why the back tick marks '' are there either? ''

    var playerID = event.target.getIframe().id; var targetInitID = ${playerID}InitStart;

    //access (get or set) alert(window[targetInitID]); window[targetInitID] = 1;

2
  • 3
    Don't. Use an object (or array) instead of 4 separate variables Commented Feb 8, 2019 at 23:36
  • Can you put that into some context? What exactly would I be putting into an array? I still need to take the returned value/variable (playerID, which will be player1, player2, player 3 or player4).. and tack on the InitStart string/extension to target a variable.. I'm failing to see how an array comes into play here? or helps? Commented Feb 8, 2019 at 23:41

1 Answer 1

1

Here we build string and address window[yourvar] to increment, does this work for you

var player1InitStart = 0;
var player2InitStart = 0;
var player3InitStart = 0;
var player4InitStart = 0;

//var playerID =  event.target.id; // will return, player1, 2, 3, 4

document.querySelectorAll('div').forEach(div => {
  div.addEventListener('click', clickEvent);
});

function clickEvent(event) {
  let playerID = `${event.target.id}InitStart`;
  window[playerID] += 1;
  logVars();
}

function logVars() {
  console.log(player1InitStart);
  console.log(player2InitStart);
  console.log(player3InitStart);
  console.log(player4InitStart);
}
<div id="player1">1</div>
<div id="player2">2</div>
<div id="player3">3</div>
<div id="player4">4</div>

Here is a solution that follows CertainPerformance advice:

var players = {
  player1InitStart: 0,
  player2InitStart: 0,
  player3InitStart: 0,
  player4InitStart: 0
};

document.querySelectorAll('div').forEach(div => {
  div.addEventListener('click', clickEvent);
});

function clickEvent(event) {
  let playerID = `${event.target.id}InitStart`;
  players[playerID] += 1;
  logVars();
}

function logVars() {
  for(let k in players) {
    console.log(players[k]);
  }
}
<div id="player1">1</div>
<div id="player2">2</div>
<div id="player3">3</div>
<div id="player4">4</div>

Sign up to request clarification or add additional context in comments.

11 Comments

while this should work, sometimes we need to not answer the direct question and instead steer OP towards a better approach. This is one of those times.
Why & where does 'window' come from? Also where does the 'let' do in the code above?
@whispers: if this javascript runs in a browser, the browser exposes global variable window
per @SergioTulentsev I agree the second solution, using an object is better. Even better would be an array of player objects.
I am just trying to set/update a variable to show that is has been played once. Why are there back ticks on the line that is concatenating the two pieces of data? I see no reason to put those variables in an array.. there will never be any looping going on to update them.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.