1

I am trying to do a simple Backpack class to review objects with vanilla javascript. While doing so, I've run into a block because I am unable to create a proper AddItem method or get my decrementItem method to work either.

My code is below:

class Backpack {
  constructor(name) {
    this.name = name;
    this.items = {};
  }

  name() {
    return `My backpack's name is ${this.name}`;
  }

  addItem(i, value) {
    return this.items[i] ? (this.items[i] += value) : (this.items[i] = 1);
  }

  decrementItem(i) {
    if (this.items[i]) {
    }
    return this.items[i] ? this.items[i]-- : (this.items[i] = 0);
  }
}

let newBackpack = new Backpack("Bacon");

newBackpack.addItem("pencil", 3);
newBackpack.addItem("book");
newBackpack.addItem("pen");
newBackpack.addItem("paper");

console.log(newBackpack.items);

I am also curious as to how I could add validation to my decrementItem method to check to see if a value doesn't go negative as that would cause some issues.

blex
  • 24,941
  • 5
  • 39
  • 72
drome845
  • 139
  • 1
  • 1
  • 8
  • do u need to store the items in an object? or u can use a simple array? – marcos Feb 01 '21 at 23:26
  • I am trying to do objects for a practice interview question – drome845 Feb 01 '21 at 23:28
  • What is your intention when you pass the value 3 to addItem? Do you care if there is already an item at index 3? Do you care of there are items in the indexes below 3? (Why not check for the next available index and then add the item at that index instead of trying to manually keep track of the indexes?) – devlin carnate Feb 01 '21 at 23:35

3 Answers3

3

You could add these changes:

  • i is often used in loops to refer to numeric indexes. It's not immediately clear that you use it for item names. Naming this variable item might make more sense.
  • In addItem, when there is no such item to start with, you always add 1, discarding the passed value. Make sure to add the value
  • In addItem, value seems to be an optional argument. You could add a default value of 1
  • When altering a quantity, you can use this shorthand to make sure the previous value is used, or zero when there is none or it's already zero: this.items[item] || 0
  • Math.max(n, 0) would allow you to make sure no value goes below zero

class Backpack {
  constructor(name) {
    this.name = name;
    this.items = {};
  }

  name() {
    return `My backpack's name is ${this.name}`;
  }

  addItem(item, value = 1) { // 1 by default if not provided
    return this.items[item] = (this.items[item] || 0) + value; // Add the value
  }

  decrementItem(item) {
    return this.items[item] = Math.max((this.items[item] || 0) - 1, 0); // 0 minimum
  }
}

let newBackpack = new Backpack("Bacon");

newBackpack.addItem("pencil", 3);
newBackpack.decrementItem("pencil");
newBackpack.addItem("book");
newBackpack.addItem("pen");
newBackpack.addItem("paper");

console.log(newBackpack.items);
blex
  • 24,941
  • 5
  • 39
  • 72
1

you must first save item to constructor

class Backpack {
  constructor(name) {
    this.name = name;
    this.items = {};
  }

  name() {
    return `My backpack's name is ${this.name}`;
  }

  addItem(i, value) {
    this.items[i] = value ? value : 1;
  }

  decrementItem(i) {
    if (this.items[i]) {
      this.items[i] = this.items[i] - 1;
    } else {
      console.log(`Item not found !!`);
    }
  }
}

let newBackpack = new Backpack("Bacon");

newBackpack.addItem("pencil", 3);
newBackpack.addItem("book");
newBackpack.addItem("pen");
newBackpack.addItem("paper");

newBackpack.decrementItem("pencil");

console.log(newBackpack.items);
Mahdi Sheibak
  • 620
  • 1
  • 4
  • 13
1

This might look nice for ur interview I guess

class Backpack {
  constructor(name){
    this.name=name
    this.items={}
    this.removeItem=function(item){
      item=item.toLowerCase()
      if(this.items[item]){delete(this.items[item])}
      else{throw Error("Cannot delete a NON-EXISTING attribute "+item)}
    }
    this.minusItem=function(item,number){
      item=item.toLowerCase()
      if(this.items[item]){this.items[item]-=number||1}
      else{throw Error("Cannot Subtract from a NON-EXISTING attribute "+item)}
    }
    this.addItem=function(item,number){
      item=item.toLowerCase()
      if(this.items[item]){this.items[item]+=number||1}
      else{this.items[item]=number||1}
    }
    this.getCountOfItems=function(item){
      item=item.toLowerCase()
      return this.items[item]||null
    }
  }
}

let newBackpack = new Backpack("Bacon");

newBackpack.addItem("pencil",3);
newBackpack.addItem("pencil",5);
newBackpack.minusItem("pencil",2);
newBackpack.addItem("book");
newBackpack.addItem("pen");
newBackpack.addItem("paper");

console.log(newBackpack.items);
console.log(newBackpack.getCountOfItems("pencil")) //6
console.log(newBackpack.getCountOfItems("lunchbox")) //null representing it not existing

try{newBackpack.minusItem("lunchbox",2)}
catch(err){console.error(err.message)}

try{console.log(newBackpack.removeItem("lunchbox"))}
catch(err){console.error(err.message)}
The Bomb Squad
  • 4,192
  • 1
  • 9
  • 17