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!

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

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.