1

I am trying to pass a function argument into a variable (if this is possible) to reduce code repetitiveness and nested if statements/ switch statements. Through research, I discovered the getElementById reference would need to be place in quotes to show up as an id. The main question is, can I pass the argument into a variable name?

<body>
<div class="contain">
    <h3 id="red">0</h3>
        <h3 id="green">0</h3>
        <h3 id="blue">0</h3>
    <button onClick="player_one.addResource(4,green)">Add Green</button>
        <button onClick="player_one.addResource(3,red)">Add Red</button>
        <button onClick="player_one.addResource(7,blue)">Add Blue</button>
</div>

<script>
    var redResource = 0;
    var greenResource = 0;
    var blueResource = 0;

    var player_one = {
    addResource: function(num,color){
        (color)Resource += num
        document.getElementById(color).innerHTML =((color)Resource);
    }

    }

 </script>
 </body>
 --------------------------------------------------------------------
 --------------------------------------------------------------------
 //with a different approach, I still have an issue
  var player_one = {
addResource: function(num,color){
        player_one.color += num
        document.getElementById(color).innerHTML =    (player_one.color);
    },

  red: 0,
  green: 0,
  blue: 0


  }
1
  • your "different approach" will not work better. see my answer below Commented Feb 28, 2019 at 17:21

3 Answers 3

3

instead of variables, you can use an Object :

var resources = {
  red: 0,
  green: 0,
  blue: 0
};

var player_one = {
  addResource: function(num, color) {
    resources[color] += num;
    document.getElementById(color).innerHTML = resources[color];
  }
};
Sign up to request clarification or add additional context in comments.

1 Comment

This one did not trigger the id correctly. However, when I tried with an id already selected, the result of the innerHTML came back as NaN.
0

this is bad coding, but here is your answser:

var
  redResource = 0,
  greenResource = 0,
  blueResource = 0
;

var player_one = {
  addResource: function(num,color){
    window[color+'Resource'] += num;
    document.getElementById(color).innerHTML = window[color+'Resource'];
  }
}
#red { color:red }
#green { color:green }
#blue { color:blue }
<div class="contain">
  <h3 id="red">0</h3>
  <h3 id="green">0</h3>
  <h3 id="blue">0</h3>
  <button onClick="player_one.addResource(4,'green')">Add Green</button>
  <button onClick="player_one.addResource(3,'red')">Add Red</button>
  <button onClick="player_one.addResource(7,'blue')">Add Blue</button>
</div>

4 Comments

This worked perfect as well. I actually came across the window[] approach at some point in my research, but could not get it to work. Can you explain why this would be considered bad coding? And the window[] approach is new to me. Is there a reference that teaches this?
I changed to this answer, as it is the correct answer for the way the question was posed. Even though the creating object approach was a better solution to my overall goal. Is using the window[ ] approach the only way to insert the argument into the variable name?
while JS is object, but in your case, you should choose to create your own object as : var Ressource = { red:0, green:0, blue:0 }; and use it like : Ressource[color] += num;
[everything in JS is object]. using window[ variable name ] is not a good idea. there is many property and methods on this top object, like window.length or window.name... (window['length'], window['name'], ...). and maybe tomorow there where window['blue'], window['red'] in the global window scope and your code will not longuer working
0

it will be better to use object for it instead of doing some trickery

var player_one = {
addResource: function(num,color){

        player_one[color] += num
        document.getElementById(color).innerHTML =    (player_one[color]);
    },

  red: 0,
  green: 0,
  blue: 0


  }

// console.log(document.getElementById('green'))
<div class="contain">
    <h3 id="red">0</h3>
        <h3 id="green">0</h3>
        <h3 id="blue">0</h3>
    <button onClick="player_one.addResource(4,'green')">Add Green</button>
        <button onClick="player_one.addResource(3,'red')">Add Red</button>
        <button onClick="player_one.addResource(7,'blue')">Add Blue</button>
</div>

what you were missing was calling the function with string arguments .. as in func(3, 'red') and not func(3, red)

and in javascript player_one[color] and not player_one.color

4 Comments

(color)Resource you missed one
This one did not trigger the id correctly. However, when I tried with an id already selected, the result of the innerHTML came back as NaN.
This worked perfect! I tried it on my own page, but it still wasn't working. But I figured out I was using double quotes within the double quotes of the button. Changed to single quotes around the color, it worked. Thank you!
sure, i am glad :)

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.