Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

brandonlind2
brandonlind2
7,823 Points

a function to remove inconstant index items in an array, is this possible?

I'm working on a simply test based rpg as a practice project, and I want to create a function that allows the player to drop items from their inventory, how could I write a function with out it dropping the first or last item each time, what if the item that needed to be dropped is indexed at 5 in an array of 10?

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

Am I going to need to create some type of complex function that cycles through everything in the array and adds them into another array until it gets to the item that needs to be droppedd then dumps the new array back into the old one once the item is dropped or is there an easier way?

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

What you're looking for is the .splice() function. Here's a link to the documentation provided by MDN.