1

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);
3
  • 1
    Consider: jsfiddle.net/userdude/sS2fY Commented Aug 16, 2012 at 1:38
  • If the curItem variable 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. If storeStock is not an array, then you would get a TypeError in the console because of the call to pop. Commented Aug 16, 2012 at 1:43
  • "Can .pop() be used on an array of objects?" - The .pop() method can be used on any array (and, indeed, on array-like objects via .call or .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. Commented Aug 16, 2012 at 2:40

3 Answers 3

2
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(stock)
    {
        if (stock instanceof Array)
        {
            for (var item in stock)
            {
                storeStock.push(stock[item]);
            }

            storeStock.reverse(); 
        };
    };

    //array out function
    var getStockList = function()
    {
        return storeStock;
    };

    var sellEverything = function(gold)
    {
        var holder;

        console.log("You decide to buy EVERYTHING.");

        while (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
    };
};


var store = new ItemStore();
store.setStockList(itemList.items);
console.log(store.getStockList());
store.sellEverything(100000);

http://jsfiddle.net/userdude/sS2fY/3/

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

6 Comments

The problem is that for the sake of staying in the rubric of the project, I kinda have to keep the model I have of putting the array in with the pushing.
See my edit. Are you saying you have to iterate over an object list with a for loop (as opposed to a for each loop)?
Note as well your object literal notation is wrong. The labels don't have "s around them. See the edit.
@Tarkenfire - And with a plain array iterator: jsfiddle.net/userdude/sS2fY/4
Basically yes. It seems that the answer to most of my problems revolved around the incorrect literal notation, so this does solve my problem. Thanks.
|
1

It looks like storeStock is an object that has an items array. If so, this should work:

var sellEverything = function(gold)
{
    console.log("You decide to buy EVERYTHING.");

    var holder;
    while (storeStock.items.length > 0)
    {
        console.log(storeStock);
        var curItem = storeStock.items.pop();
        console.log(curItem);
        console.log("You buy a " + curItem.itemName + " for " + curItem.itemPrice + " gold.");
        gold -= curItem.itemPrice;
        console.log(gold + " gold remaining");
    };
};

2 Comments

I just edited my post to show more of the code, but suffice to say I populated the array with only the .items stuff before, sorry for causing confusion.
@Tarkenfire - No worries. Glad you found a solution.
0

storeStock is an object with storeStock.items being the array you want

Comments

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.