Skip to content

Instantly share code, notes, and snippets.

@abu-hasib
Last active November 16, 2022 10:23
Show Gist options
  • Save abu-hasib/89875a0cce90b3b8bdc26ae5e08c6767 to your computer and use it in GitHub Desktop.
Save abu-hasib/89875a0cce90b3b8bdc26ae5e08c6767 to your computer and use it in GitHub Desktop.
function inventory() {
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
}
}
}
@adeleke5140
Copy link

Thank you very much. I had an idea of solving it this way but didn't really know how to write the code.

If you don't mind, how do you approach problem solving?

@Snehabhatt61
Copy link

How we call the remove function here?

@Inno-cent
Copy link

Thanks for this solution, really helped

@zsiddiqui22
Copy link

zsiddiqui22 commented Aug 14, 2022

// make object
const a = new inventory();

// Add item
a.add('zohaib');

// view List
a.items; OR a.getList();

// Remove Item
a.remove('zohaib');

https://zsiddiqui.me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment