Skip to content

Instantly share code, notes, and snippets.

@adeleke5140
Last active May 8, 2022 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adeleke5140/efe999f70c071fc26fa811f2c996949f to your computer and use it in GitHub Desktop.
Save adeleke5140/efe999f70c071fc26fa811f2c996949f to your computer and use it in GitHub Desktop.
An InventoryList to add, remove and get items in an inventory
function inventoryList(){
return {
items: [],
add: function(item){
if(this.items.includes(item)){
return
}
this.items.push(item)
},
remove: function(item){
if(this.items.includes(item)){
let index = this.items.indexOf(item)
if(index > -1){
this.items.splice(index, 1)
}
}
},
getList: function(){
return this.items
},
}
}
//have an inventory, a add function and a remove function, getList, return the items
//in the inventory
const inventory = inventoryList();
// console.log(inventory.getList())
inventory.add('ketchup');
inventory.add('mayo')
inventory.add('mustard')
inventory.remove('mayo');
inventory.remove('tomato')
console.log(inventory.getList())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment