Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Can someone telling me why my .splice() method isn't working?

I'm getting an unexpected syntax error. I'm not sure whats wrong with the code I'm creating a simply video game and i need the inventoryDrop method to drop an object passed to the drop parameter. sorry for whatever reason I cant get the javascript markdown to work.

var player={
    name: ' ',
    level: 1,
    health: 100,
    inventory: [],
    equipedWeapon: '',
    armor: '',
    money: 0,
    inventoryAdd: function(add){
    player.inventory.push(add);
    }
        inventoryDrop: function(drop){
    var dropIndex= player.inventory.indexOf(drop);
    player.inventory.splice(dropIndex,1);
        }
};

1 Answer

You just forgot a comma before the inventoryDrop function. Here is the code correctly formatted.

var player={
    name: ' ',
    level: 1,
    health: 100,
    inventory: [],
    equipedWeapon: '',
    armor: '',
    money: 0,
    inventoryAdd: function(add){
    player.inventory.push(add);
    },
    inventoryDrop: function(drop){
    var dropIndex= player.inventory.indexOf(drop);
    player.inventory.splice(dropIndex,1);
    }
};

haha thanks man!