0

this is my javascript

var inventory = [];
var money = 10;
function buyToInventory(what){
    if(this.price>money){
        log.unshift("D"+day+" "+hour+":"+minute+" - Poor hobo, you can't afford "+this.name+".<br>");
    } else {
        inventory.push=what;
        money=money-this.price;
            updateMoney();
    };
};
function drinkable(name, price, effect){
    this.name = name;
    this.price = price;
    this.effect = effect;
    this.use = function(){
        satisfy(thirst,this.effect,"#thirst")
    };
};
var water = new drinkable("1l water", 1, 20);

Problem is when I write this in console buyToInventory(water). It seems like my function buyToInventory is totally wrong, it doesnt push water into the inventory array or when I set money = 0, it doesn't add that message about not having money. I can't figure out what am I doing wrong. Any help will help me a lot. :)

Thanks.

1
  • 2
    I think you likely mean to use what.price instead of this.price. Here, if you really are calling buyToInventory(water), then this is the global window object inside buyToInventory. Commented Nov 26, 2013 at 21:09

1 Answer 1

2

To use push, to push an item into the array, you need to do:

inventory.push(what);

Or whatever part of what you need e.g. price as mentiond by @apsillers

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

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.