I'm writing some code for a class project, and am running into the a problem with the following code:
var sellEverything = function(gold)
{
console.log("You decide to buy EVERYTHING.");
var holder;
while (storeStock.length > 0)
{
console.log(storeStock);
var curItem = storeStock.pop();
console.log(curItem);
console.log("You buy a " + curItem.itemName + " for " + curItem.itemPrice + " gold.");
gold -= curItem.itemPrice;
console.log(gold + " gold remaining");
};
};
For perspective, storeStock is an array with 6 objects in it. The curItem variable is coming up in the console as undefined, so is it even possible to pop objects off of arrays or is the problem something else?
Here is the data with which storeStock is populated with:
"items":
[
{"itemName":"Sword", "itemPrice":100},
{"itemName":"Bow", "itemPrice":240},
{"itemName":"Shield", "itemPrice":120},
{"itemName":"Lance", "itemPrice":300},
{"itemName":"Potion", "itemPrice":50},
{"itemName":"Gem of supreme power", "itemPrice":5},
{"itemName":"Better movie script", "itemPrice":139083882}
]
Sorry in advance if it's an obvious question.
Edit: Okay, I feel I need to show the entirety of the code for the sake of clarity:
var itemList =
{
"items":
[
{"itemName":"Sword", "itemPrice":100},
{"itemName":"Bow", "itemPrice":240},
{"itemName":"Shield", "itemPrice":120},
{"itemName":"Lance", "itemPrice":300},
{"itemName":"Potion", "itemPrice":50},
{"itemName":"Gem of supreme power", "itemPrice":5},
{"itemName":"Better movie script", "itemPrice":139083882}
]
};
//private class ItemStore
var ItemStore = function()
{
var storeStock = [];
//array in function
var setStockList = function(stockArray)
{
if (stockArray instanceof Array)
{
var i = stockArray.length;
for (i; i > 0; i--)
{
storeStock.push(stockArray[i]);
}
//yes, I could have just done the loop forwards, but wanted to do it this way.
storeStock.reverse();
};
};
//array out function
var getStockList = function()
{
return (storeStock);
};
var sellEverything = function(gold)
{
console.log("You decide to buy EVERYTHING.");
var holder;
while (storeStock.length > 0)
{
console.log(storeStock);
var curItem = storeStock.pop();
console.log(curItem);
console.log("You buy a " + curItem.itemName + " for " + curItem.itemPrice + " gold.");
gold -= curItem.itemPrice;
console.log(gold + " gold remaining");
};
};
return{
"setStockList": setStockList,
"sellEverything": sellEverything,
"getStockList":getStockList
};
};
Also of note is how I call the setStockList method:
store.setStockList(itemList.items);
curItemvariable is showing up as undefined in the console, then you are in the loop. There must be some other code that is causing the problem. IfstoreStockis not an array, then you would get a TypeError in the console because of the call topop..pop()method can be used on any array (and, indeed, on array-like objects via.callor.apply), regardless of the type of the elements. Remember also that a JS array can hold elements of different types, i.e., some elements might be objects, some might be numbers, etc.