5. Bleep Bleep - this might be helpful


#1

Hi there - i found i got quite stuck on this one, so i thought i'd post a stripped down version of it(slightly diff variable names, for education purposes only) - just if it's helpful to anyone similarly stuck as i was.

Key things i learned that made it clearer were:

  • remembering to call the voiding function with the cashRegister. prefix. Which sounds so simple... because it is. but that cost me 20 minutes of debugging to see it.

  • also declaring the lastItemScanned at the top first, to keep it accessible/updatable from within the properties/functions underneath.

var cashRegister = {

  total: 0,
  lastItemScanned : 0,

  add: function(itemCost) {
      this.total += itemCost;
	  this.lastItemScanned = itemCost;
  },
  
  voidItem: function() {
    this.total -= this.lastItemScanned;
    console.log('1 item voided +-+-+-', this.lastItemScanned);
  },

  scan: function(item) {
      switch(item) {
        case "eggs" : this.add(2.99);
          break;
        case "bacon" : this.add(3.99);
          break;
        case "beer" : this.add(4.99);
          break;
      }
    console.log(item, '++ just scanned ++');
    },
};

cashRegister.scan("eggs");
cashRegister.scan("bacon");
console.log("total: ", cashRegister.total);
cashRegister.voidItem();
console.log('new total', cashRegister.total);

#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.