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!

1
  • Welcome to SO! Nice amount of details and code snippets. Keep it up! Commented Jan 8, 2013 at 8:01

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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!

3 Comments

OOP, good call looks like Jacco just posted about that I will have to take a look thanks for the response!
Jacco used an array, it is an 'in between' :)
Thanks for your help, I could not seem to get the eval() to work :(
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>";

6 Comments

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!
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" ?
Yes, this is classified as a jagged array: stackoverflow.com/questions/5939504/… . 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.
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 Sounds like you need to read up on Objects and Arrays in JavaScript; you seem to be missing some fundamental knowledge.
|

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.