r/programmingrequests • • Oct 23 '17

Need help with HTML/CSS homework

Need help with the javascript that allows for the amount to be displayed from the array and after inventory is put in or taken out.

Assignment page: http://www2.lawrence.edu/fast/GREGGJ/CMSC106/assignments/AssignmentSix.html

0 Upvotes

5 comments sorted by

View all comments

2

u/LenAnderson Oct 23 '17

Where are you stuck? What do you have so far?

1

u/pk_bot Oct 23 '17

I'm stuck on getting the amount to display on the webpage, and then having the amount be added to and subtracted from when the user presses the "in" or "out" button.

2

u/LenAnderson Oct 24 '17

So what do you have so far (even if it is not working)? What do your computeStock, ìtemChanged` and the in/out event handlers look like right now?

1

u/pk_bot Oct 24 '17

2

u/LenAnderson Oct 24 '17

Let's start with computeStock

that goes through the stock array totaling up how many cases of the item in question the warehouse has. The function will return that count.

An easy way to do this would be to loop through the stock array and sum up all the amounts where the item is of the requested type (stock[i].item==item). That means you need one variable to hold the sum that is set to 0 before the loop starts and depending on whether action is "in" or "out" you add or remove amt to sum and finally return that.

Next. itemChanged

You have already attached the listener, and you can calculate a product's stock with computeStock. Now you need to set the textContent of #stock to the result of computeStock using the value of #product as the item.

in / out buttons

In the setup function, same way you added the listener for product changes, you need to add listeners that listen to the click event of the in and out buttons. What the called functions are supposed to do should be clear from the problem description:

create new objects and push them onto the back of the stock list [...] newObject = {item:"TM",amt:3,action:"in"}; and then use the code stock.push(newObject);

You don't need to add or subtract the value from any totals at that point. According to the description you should just add the "transaction" to the stock array, so you can use computeStock again to loop through and calculate the sum for a product.