Last active
May 8, 2022 12:06
-
-
Save adeleke5140/efe999f70c071fc26fa811f2c996949f to your computer and use it in GitHub Desktop.
An InventoryList to add, remove and get items in an inventory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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