I'm trying to condense my code and I've looked at a number of articles on concatenating variables but I haven't been able to get anything to work the way I need it to.
Below are two snippets of code. They are functionally identical except the first snippet has variables that use 'Str' in their name and the second snippet uses 'Dex' instead. I plan on creating similar code with 'Int' and 'Ht' but I don't want to copy / paste and then go back to change the 'Str, Dex, Int, Ht' names all over my code over and over again.
Is it possible to put the 'Str, Dex, Int, Ht' into a variable [var1] (according to what button is pressed) and then use [var1] in place of 'Str, Dex, Int, Ht' so that the code only has to be written once?
//Define Stength Increase Function
var strIncrease = function () {
//Increase playerStrength by 1
playerStr = playerStr + 1;
playerStrDisplay.innerHTML = playerStr;
//Increase strengthCP by 10
strCP = strCP + strCost;
strCPDisplay.innerHTML = strCP;
//Decrease totalCP by 10
totalCP = totalCP - strCost;
totalCPDisplay.innerHTML = totalCP;
}
//Define Dexterity Increase Function
var dexIncrease = function () {
//Increase playerDexterity by 1
playerDex = playerDex + 1;
playerDexDisplay.innerHTML = playerDex;
//Increase DexterityCP by 20
dexCP = dexCP + dexCost;
dexCPDisplay.innerHTML = dexCP;
//Decrease totalCP by 20
totalCP = totalCP - dexCost;
totalCPDisplay.innerHTML = totalCP;
}
var o = {}; o.foo = 'foo'; console.log(o['foo']); /* foo */ }