2

I am working on a replica of an old game I played in High School called Drug Wars. I have global variables that hold the qty of each drug owned. Originally I just used a different function for each buy and sell operation for each drug. Trying to save myself some code and provide for some scale ability I wanted to change it to one function for buying, passing the drugqty, drugprice(done by a different function), and the drugname.

The part I am stuck with is trying to update the drugqty (used locally in the function) to the global variable.

function buy(drugqty, drugprice, drugname)
{
if (money < drugprice)
{
    window.alert("You do not have enough money to do that!")
}
else if(money >= drugprice)
{
var maxdrug = money/drugprice;
var addtoqty;
addtoqty=prompt("How many would you like to purchase? They are $" + drugprice +" a unit. You can afford " + Math.floor(maxdrug) + "." );
if((addtoqty*drugprice)>money)
{
    window.alert("Nice try you need more money to do that!")    
}
else {

drugqty = parseInt(drugqty, 10) + parseInt(addtoqty, 10);
money =  money - (drugprice*addtoqty);
document.getElementById(drugname+"qty").innerHTML=drugqty;
document.getElementById("money").innerHTML=money;

}
}

}

Below is my buy button for one particular drug.

<td><center><button onclick="buy(cocaineqty, cocaine, 'cocaine')">Buy</center></button></center></td>

When the button is pressed it calls the function and executes correctly.

I just need to figure out how to update the global variable cocaineqty with what the local function drugqty creates.

I tried things like

drugname+"qty"=drugqty;

My two thoughts were to parse that information again from where it is displayed or somehow update the global variable in the function(again using it for more than one drug in the future so it can be done dynamically)

<td id="cocaineqty" align="center">0</td>

My first post here but I am more than willing to make changes or correct any mistakes with the post. Thanks for your time!

edit: Updated my code for the correct information based on the checked answer

function buy(drugqty, drugprice, drugname)
{
if (money < drugprice)
{
    window.alert("You do not have enough money to do that!")
}
else if(money >= drugprice)
{
var maxdrug = money/drugprice;
var addtoqty;
addtoqty=prompt("How many would you like to purchase? They are $" + drugprice +" a unit. You can afford " + Math.floor(maxdrug) + "." );
if((addtoqty*drugprice)>money)
{
    window.alert("Nice try you need more money to do that!")    
}
else {

drugqty = parseInt(drugqty, 10) + parseInt(addtoqty, 10);
money =  money - (drugprice*addtoqty);
document.getElementById(drugname+"qty").innerHTML=drugqty;
document.getElementById("money").innerHTML=money;
window[drugname+"qty"]=drugqty;

}
}

Thanks again for all of your assistance!

mcfunk
  • 23
  • 4

3 Answers3

1

Firstly they way this im implemented is not awesome. There is a lot that could be done better but Im not going to try to change you implementation here... :)

To do what you want to do, that is access a global variable without knowing its actual name, can be done like this.

window[drugname + "qty"] = drugqty;

This works because

  1. "Global" variable in a browser are actually on the window object

  2. objects in JavaScript are associative arrays. Which in simple terms means you can access its properties via it name as you would in a Key Value pair

Maurice Butler
  • 414
  • 2
  • 9
  • For my poorly implemented idea this was the solution I was looking for. Thank you for your help. I would be curious as to how you would change some of the implementation or maybe just ideas and what to look at correcting. – mcfunk Jan 08 '13 at 17:41
0

You can use the eval() function for this:

eval(drugname+"qty"=drugqty);

Note: It would be much better to learn about Object Oriented programming. So you can make a Drug class and class instances (objects) of this class to store the particular drug information. It is easy pass around these classes and keep track of the information!

Community
  • 1
  • 1
Veger
  • 37,240
  • 11
  • 105
  • 116
0

You could save all your quantities in a single object and add a property per drug.

var quantities = {};
quantities[drugname + "qty"] = drugqty;

But, if you want to display the results also, it might be easier to leave out the "qty"-part:

var quantities = {};
quantities[drugname] = drugqty;

And for displaying the results:

var html = "<table>";
for (var quantityName in quantities) {
  if (quantities.hasOwnProperty(quantityName)) {
    html += "<tr><td>" + quantityName + "</td>";
    html += "<td id='" + quantityName + "qty' align='center'>" + quantities[quantityName] + "</td></tr>";
  }
}
html += "</table>";
Jacco
  • 3,251
  • 1
  • 19
  • 29
  • I will have to dig in to this a bit more but I believe this is where I need to head with it. Thanks for your time and response! – mcfunk Jan 08 '13 at 08:03
  • I have a few questions about this one for you, this is making an array, would it be classified as a jagged array? Again I am just learning here but it seems like the size is not defined, and are the locations in the array what are created with the line "quantities[drugname+"qty"] = drugqty" ? – mcfunk Jan 08 '13 at 17:54
  • Yes, this is classified as a jagged array: http://stackoverflow.com/questions/5939504/how-to-initialize-a-jagged-array-in-javascript . Can you clarify the question "are the locations in the array what are created with the line..."? I don't understand what information you are looking for. – Jacco Jan 08 '13 at 19:05
  • I was just thinking a bit too hard about it I suppose, but I was just wondering if the information in the bracket "[drugname+"qty"]" was the index in the array. Possibly allowing me to set the quantities and ref them by that notation. There is so much information out there that I just confuse myself sometimes trying to incorporate it all. Sorry if this sound more confusing, but thanks again for your help! – mcfunk Jan 08 '13 at 20:38
  • @mcfunk Sounds like you need to read up on `Objects` and `Arrays` in JavaScript; you seem to be missing some fundamental knowledge. – Evan Davis Jan 08 '13 at 20:48
  • I couldn't agree more. I have been reading over at W3schools, do you have any other resources you would recommend? – mcfunk Jan 08 '13 at 21:00