Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8380723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:31:46+00:00 2026-06-09T16:31:46+00:00

I’m writing some code for a class project, and am running into the a

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:31:48+00:00Added an answer on June 9, 2026 at 4:31 pm
    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/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.