Is it possible to create a variable name with input from other variables as described below? Looking at other similar threads, it seems not recommended to use [eval].
I do understand that at a certain amount of variables, it would probably make more sense to group the content in an array, but currently I would like to start understanding if it is possible to solve by building a variable name from other variables.
Below is my code and attempt, using "template literals":
// Players:
let p1 = "player1";
let p2 = "player2";
let activePlayers = p2;
console.log("Active player: ", activePlayers);
if (activePlayer == "p1") {
let score_(${p1})_upper_row1;
let score_(${p1})_upper_row2;
let score_(${p1})_upper_row3;
} else if (activePlayer == "p2") {
let score_(${p2})_upper_row1;
let score_(${p2})_upper_row2;
let score_(${p2})_upper_row3;
}
Expected produced variable names:
If player is set to [player1]:
score_player1_upper_row1;
score_player1_upper_row2;
score_player1_upper_row3;
If player is set to [player2]:
score_player2_upper_row1;
score_player2_upper_row2;
score_player2_upper_row3;