BLEEP BLEEP "Ooop try again. False" problem


#1


Excersise 5.


After my code I got this error massage: "Ooops try again. False"


My code:

var cashRegister = {
    total:0,
    //Dont forget to add your property
    add: function(itemCost, lastTransactionAmount) {
        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
var voidLastTransaction = function(){
    cashRegister.total = (cashRegister.total - cashRegister.lastTransactionAmount)
};
    
    

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

//Void the last transaction and then add 3 instead
voidLastTransaction()
cashRegister.scan('chocolate', 3);

//Show the total bill
console.log('Your bill is '+cashRegister.total);

And the result 8.55 is printed.
I think result 8.55 is good, but still got this error.
Please help. :slight_smile:


#2

I see two problems here:
1. you do not need to use 'lastTransactionAmount' argument in your 'add' function
2. You should use two argument; 'itemCost' and 'quantity' in your 'voidLastTransaction' function. Then apply "this.total-=quantity*itemCost"


#3

After your comments:

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

var voidLastTransaction = function(itemCost, quantity){
this.total-=quantity*itemCost
};

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

//Void the last transaction and then add 3 instead
voidLastTransaction()
cashRegister.scan('chocolate', 3);

//Show the total bill
console.log('Your bill is '+cashRegister.total);

Result is 10.35 (I think it's wrong) and error is still the same.


#4

See my #1 comment:
You should delete the second row on your add function. It should be like:
add: function(itemCost) {
this.total += itemCost;
},


#5

This don't work for me. I got either result 10.55 or error massage after your corrections.

I don't want to copy code from some Q&A topic. That's not the point of this course. I want to understand this.
This is my code. I don't know where mistake is:

var cashRegister = {
    total:0,
    lastTransactionAmount: 0,
    //Dont forget to add your property
    add: function(lastTransactionAmount) {
        this.total += lastTransactionAmount
        this.lastTransactionAmount = lastTransactionAmount
        
    },
    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
var voidLastTransaction = function(){
    cashRegister.total = (cashRegister.total - cashRegister.lastTransactionAmount)
};

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

//Void the last transaction and then add 3 instead
voidLastTransaction()
cashRegister.scan('chocolate', 3);

//Show the total bill
console.log('Your bill is '+cashRegister.total);

#6

Hi maciejjy,

Put the voidLastTransaction method as a property inside of the cashRegister object. You will then want to use the same format as you did for the add and scan properties (you do not need to declare it as a "var" and will want to change the "=" to a ":").

To void the last transaction, use cashRegister.voidLastTransaction(); rather than voidLastTransaction().

I hope this helps!


#7

Thank you. It worked, but honestly I don't know what was the problem.
Result was the same with my code. :expressionless:


#8

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