Build a Register: Bleep Bleep (Needs Debug)


#1


var cashRegister = {
    total:0,
    //Dont forget to add your property
    add: function(itemCost, lastTransactionAmount) {
        this.total +=  itemCost;
        lastTransactionAmount = itemCost;
    },
    scan: function(item,quantity) {
        switch (item) {
        case "eggs": this.add(0.98 * quantity); break;
        case "milk": this.add(1.23 * quantity); break;
        case "magazine": this.add(4.99 * quantity); break;
        case "chocolate": this.add(0.45 * quantity); break;
        }
        return true;
    },
    //Add the voidLastTransaction Method here
    voidLastTransaction: function(lastTransactionAmount){
    this.total= add.this.total -= add.lastTransactionAmount;
    }
};

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',1);
cashRegister.scan('chocolate',4);


#2

total:0,
//initiate lastTransactionAmount here
    //Dont forget to add your property
    add: function(itemCost, lastTransactionAmount) {
        this.total +=  itemCost;
        lastTransactionAmount = itemCost;
        //good

voidLastTransaction: function(lastTransactionAmount){
    this.total= add.this.total -= add.lastTransactionAmount;
    //wrong, you need to substract lastTransactionAmount from total, so
    //this.total -= this.LastTransactionAmount
    }

Hope I could help .)


#3

Does lastTransactionAmount have to be a parameter in the voidLastTransaction function?


#4

==several sections==
==S1== function vs method
An object has one or more properties, separated by a comma_,

A property has a property-key and it's associated VALUE
the property-key is also used as the property-NAME

The associated property-VALUE can be anything from:

‚ÄĘNumber
‚ÄĘString
‚ÄĘBoolean
‚ÄĘSymbol
‚ÄĘObject  ‚ó¶Function/'Method'
         ‚ó¶Array
         ‚ó¶Date
         ‚ó¶RegExp
         ‚ó¶Math
‚ÄĘNull
‚ÄĘUndefined

if the associated property-Value is a function
we now call it a Method

Thus your question should have been
Does lastTransactionAmount have to be a parameter in the voidLastTransaction Method?
instead of your
Does lastTransactionAmount have to be a parameter in the voidLastTransaction function?

==S2== using lastTransactionAmount as PARAMETER in the voidLastTransaction() Method
The voidLastTransaction PARAMETER will be used as a =local= VARIABLE within the voidLastTransaction() Method
you would then have re-write the voidLastTransaction() Method
like

voidLastTransaction: function(lastTransactionAmount){
    this.total -= lastTransactionAmount;
    }

and as the (voidLastTransaction()_ Method is defined with 1 PARAMETER
when =calling= the voidLastTransaction() Method you will have to provide 1 ARGUMENT
and the ARGUMENT should be a number-Value
like
as @javamaster95566 correctly noticed the =call= should be
cashRegister.voidLastTransaction(cashRegister.lastTransactionAmount);


#5

Hi, I wonder that don't we need to call the void method like this?

cashRegister.voidLastTransaction(cashRegister.lastTransactionAmount);


#6

edited/corrected my =Post=...
I just wanted to give an Extra.

If you use the code

var cashRegister = {
    total:0,
    //Dont forget to add your property
    add: function(itemCost) {
        this.total +=  itemCost;
        this.lastTransactionAmount = itemCost;
    },
    scan: function(item,quantity) {
        switch (item) {
        case "eggs": this.add(0.98 * quantity); break;
        case "milk": this.add(1.23 * quantity); break;
        case "magazine": this.add(4.99 * quantity); break;
        case "chocolate": this.add(0.45 * quantity); break;
        }
        return true;
    },
    //Add the voidLastTransaction Method here
    voidLastTransaction: function(lastTransactionAmount){
       this.total -= this.lastTransactionAmount;
    }
};
console.log(cashRegister);
cashRegister.scan('eggs',1);
console.log(cashRegister);
console.log("==============================");
console.log(cashRegister.scan.toString());

for the first console.log() you would get

{ total: 0, 
  add: [Function], 
  scan: [Function], 
  voidLastTransaction: [Function] }

The cashRegister-Object has 1 =property= and 3 =Methods=

and for the second console.log() just after your call of the cashRegister.scan() Method
you would get

{ total: 0.98,
  add: [Function],
  scan: [Function],
  voidLastTransaction: [Function],
  lastTransactionAmount: 0.98 }

The cashRegister-Object now has 2 =properties= and 3 =Methods=

With the third console.log() Method
you will actually see the scan Method definition.


#7

Thank you. That's very clear.
The definition of scan Method is exactly what we have written!


#8

And to make the cashRegister Object complete,
i would suggest to re-init the lastTransactionAmount property to 0 zero
in your voidLastTransaction Method
like

voidLastTransaction: function() {
    this.total -= this.lastTransactionAmount;
    this.lastTransactionAmount  = 0;
}

to Ensure that multiple =calling= of the voidLastTransaction() Method does not have an impact on the =total= (it can only be used to cancel the last-scan once !! )


#9

cashRegister.voidLastTransaction();
cashRegister.scan('chocolate', 3);

For someone it will be useful.


#10

cashRegister.scan(‚Äėchocolate‚Äô,3);

is not working…it shows
"Oops, try again. false"
message…

In instructions they say…
‚ÄúThen use the new method to void the last item we scanned. Finally, scan only 3 of the same item instead.‚ÄĚ

What does this mean if ‚ÄúcashRegister.scan(‚Äėchocolate‚Äô,3);‚ÄĚ code is wrong?
please help I am not getting this part!!


#11

hey sorry its working…I have written the wrong function name…
the code works fine!!


#12

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