0

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;
}
2
  • Yes: with a little bit of refactoring you should be able to leverage the fact that properties on objects are addressable via strings. var o = {}; o.foo = 'foo'; console.log(o['foo']); /* foo */ } Commented May 31, 2015 at 19:46
  • 2
    You should look into learning Object-oriented programming, especially if you're making games. Commented May 31, 2015 at 19:48

3 Answers 3

3

You can use an object as a map:

var player = { str: 0, dex: 0, ... };

function increase(stat) {
    player[stat]++;
    document.getElementById(stat + 'Display').innerHTML = player[stat];
}

increate('str');
Sign up to request clarification or add additional context in comments.

1 Comment

What you call a map is just an object literal. Maps are something slightly different, and are part of the ES6 proposal.
0

You could define the attributes as properties of objects and access them using strings, like this:

var player = {
    str: 1,
    dex: 1,
    in: 1,
    ht: 1,
    strCP: 10,
    dexCP: 10,
    intCP: 10,
    htCP: 10,
    totalCP: 0
},
costs = {
   strCost: 10,
   dexCost: 10,
   inCost: 10,
   htCost: 10
};

function increaseAttribute(player, costs, attr) {
    player[attr] += 1;
    player[attr + "CP"] += costs[attr + "Cost"];
    player.totalCP += costs[attr + "Cost"];
.
.
}

function increaseStr () {
   increaseAttribute(player, costs, attr);
}
.
.
.

1 Comment

Thank you for taking the time to write this example. It makes the most sense to my newbly programming mind.
0

You can use closure to generate method dynamically.

For example.

var player = { 
   str : 0
   dex : 0
   int : 0
   ht : 0
}

function methodFactory (player, property, diff) {
    var mathod = function () {
        player[property] += diff;
    }
    return method;
}

then

var addInt = methodFactory (player, 'int', 1);
var addIntBy2 = methodFactory (player, 'int', 2);

addInt();

Write once, use everywhere

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.