Please help. This is confusing to me. Did I miss whole scan function?


#1

var cashRegister = {
total: 0,
add: function(itemCost){
this.total += itemCost;
}
};
scan: function (item) {
switch (item) {
case "eggs":
this.add(0.98);
break;

    case "milk": 
        this.add(1.23); 
        break;

    case "magazine":
        this.add(4.99);
        break;

    case "chocolate":
        this.add(0.45);
    break;
    }
        };

cashRegister.scan("eggs", 2);
cashRegister.scan("magazine", 3);
console.log('Your bill is '+cashRegister.total);


#2

Hello, @byterunner53762. You have few errors:

var cashRegister = {
   total: 0,
   add: function(itemCost){
      this.total += itemCost;
   }, // -> missed ,
}; // -> remove this line
   scan: function (item) {
      switch (item) { 
         case "eggs": 
            this.add(0.98); 
            break;
         case "milk": 
            this.add(1.23); 
            break;
         case "magazine":
            this.add(4.99);
           break;
         case "chocolate":
            this.add(0.45);
            break;
       }
   } // missed }
};

cashRegister.scan("eggs", 2);
cashRegister.scan("magazine", 3);
console.log('Your bill is '+cashRegister.total);

#3

Oh my gosh! Thank you! (actually makes me feel better because my code was ok.) Cheers


#4

One thing I also changed was this

cashRegister.scan("eggs", 2);
cashRegister.scan("magazine", 3); to this

cashRegister.scan("eggs");
cashRegister.scan("eggs");
cashRegister.scan("magazine");
cashRegister.scan("magazine");
cashRegister.scan("magazine");

With the D.R.Y, rule in mind, is there a way to shorten the above code (which works, btw)?

Thank you


#5

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