This code doesn't work, and I really need help


#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);
cashRegister.voidLastTransaction;
cashRegister.scan('chocolate',3);


#2

here

add: function(itemCost, lastTransactionAmount) {

you don't need to take lastTransactionAmount as parameter.remove it. lastTransactionAmount should be global not local one.

have a look at this line of code

this.total= add.this.total -= add.lastTransactionAmount;

It doesn't make any sense.what did you try to do here?

and here

cashRegister.voidLastTransaction;

we use () at the end to call a method.


#3

here:

lastTransactionAmount = itemCost;

currently, lastTransactionAmount has a local scope. It only exist inside the add method. to ensure we can access variable in other methods, use this

here:

voidLastTransaction: function(lastTransactionAmount)

why does your method have a parameter? We want to let the add method handle to give lastTransactionAmount a value, we don't want to supply this argument when calling voidLastTransaction

here:

this.total= add.this.total -= add.lastTransactionAmount;

add has no bussines here, the only purpose of add it to give use this.lastTransactionAmount so we know the amount of last transaction.

combined with @rcodeman answer, i think we covered all the mistakes in there


#4

okay. I think I got it.


#5

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(){
//What do I put here?
}

};

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


#6

//What do I put here?

we need to reduce total by lastTransactionAmount, don't forget about variable scope


#7

-= lastTransactionAmount;

What would I put to call the total?


#8

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(){
 cashRegister.total -= cashRegister.lastTransactionAmount;
}

};

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


#9

possible, but given method is part of object i would prefer to use this rather then cashRegister

we went to so much trouble to add this properties to this in the add method


#10

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