Bleep Bleep


#1
var cashRegister = {
    total:0,
    lastTransactionAmount: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;
    },
    voidLastTranscation: function(){
        this.total -=this.lastTransactionAmount
    }
    //Add the voidLastTransaction Method here
    
    
};

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',1);
cashRegister.scan('chocolate',4);
cashRegister.voidLastTranscation();
cashRegister.scan('chocolate',3);
//Void the last transaction and then add 3 instead


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

it returns the "Oops, try again. false". I don't know why???


#2

you have forgotten semicolon


#3

Hi @digitalslayer90169

U didn't spell right. In line 18. write voidLastTransaction: function() { instead


Same in line 30. when u call function.
Actually, ur code will work fine in another place, but in this task u need to write as they ask in instructions.


#4

Check what's written here:

http://www.w3schools.com/js/tryit.asp?filename=tryjs_local_global

This works:

var cashRegister = {
total:0,
//Dont forget to add your property
add: function(itemCost) {
lastTransactionAmount = itemCost;
console.log(lastTransactionAmount);
this.total += 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;
}
last = item;
return true;
},
//Add the voidLastTransaction Method here
voidLastTransaction: function(){
this.total-=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

console.log(cashRegister.total);
cashRegister.voidLastTransaction();

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


#5

This does not work for me.

I get:

9
ReferenceError: lastTransactionAmount is not defined


#6

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

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


#7

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

};

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

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


#8

someone help me,what is wrong ?


#9

Under your add method, you are using lastTransaction instead of lastTransactionAmount.


#10

Here is the code that works perfect:

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

cashRegister.scan('chocolate',3);


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

#11

Here is the code that works perfect:

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

cashRegister.scan('chocolate',3);


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

#12

I have a few questions about your code:

when adding a property they normally are followed with ' : ' however within the add method the lastTransactionAmount was followed by ' = ', why is this ? When I did it like this it kept coming up with errors.

Also, I'm a little confused about when to use this. and when not to, I understand from the previous lessons that .this has been used to make a method work for many objects which is why they are helpful within object consructors, but in this case it confuses me for example how to know that you needed to use it for lastTransactionAmount and why it doesn't work without it,

Many Thanks.


#13

If you look at the above code, here we are declaring a property which is followed by ':'

And the reason why lastTransactionAmount is followed by '=' in the add method is because we are using that property (not declaring as we have already done it).

And since we are accessing it inside a method of an object, we use 'this' which means whenever we are creating a method for an object and want to access a property of that object inside method, we use 'this' followed by the property name. It would ensure that we are setting a property of an object even when the parameter names are same as the property itself.

Hope this make sense!! Let me know if you still has any doubts about it.

Thanks


#14

For who wants to play and see more in this example, run this more verbose code to better understand what is happening, but it will not work as the solution.

var cashRegister = {

    total: 0,
    lastTransactionAmount: 0,

    add: function (itemCost) {
        this.total += itemCost;
        this.lastTransactionAmount = itemCost;
    },
    scan: function (item, quantity) {
        switch (item) {
            case "eggs": this.add(1 * quantity); 
            console.log("+ eggs: $1 * " + quantity)
            break;
            case "milk": this.add(2 * quantity); 
            console.log("+ milk: $2 * " + quantity)
            break;
            case "magazine": this.add(3 * quantity); 
            console.log("+ magazine: $3 * " + quantity )
            break;
            case "chocolate": this.add(4 * quantity); 
            console.log("+ chocolate: $4 * " + quantity)
            break;
        }
        return true;
    },
    //Add the voidLastTransaction Method here
    voidLastTransaction: function (lastTransactionAmount) {
        console.log("----------")
        console.log("Total - LastTransaction: " + this.total + " - " + this.lastTransactionAmount)
        this.total -= this.lastTransactionAmount;
        console.log("Total after removing: " + this.total)
        console.log("----------")
    }

};

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

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

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

#15

This one is works!

var cashRegister = {
total:0,
lastTransactionAmount: 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;
},

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

};

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

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

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


#16

I wanted to ask this question in the "You Deserve It" section but it has closed down.

i noticed that in the lesson "You Deserve it", the method's "add" and "voidLastTransaction" are built differently from what i wrote in the previous lesson:

add: function(itemCost){
this.total += (itemCost || 0);
this.lastTransactionAmount = itemCost;

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

can someone explain why "add" has the conditional (itemCost || 0)
and does (this.lastTransactionAmount = 0) just reset "lastTransactionAmount" to zero in the instance it may be used again?

hope this is clear


#17

var cashRegister = {
total:0,
lastTransactionAmount: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() {
this.total -= this.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
cashRegister.voidLastTransaction();
cashRegister.scan('milk',3);

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

What am I doing wrong here? Error says "Oops, try again. false"


#18

Thank you. This was helped me.