Bleep Bleep: it seems to work but it won't let me go through


#1

I'd be grateful if anyone could tell me what's wrong with my code, because it seems to work as it outputs the total just fine. Thanks

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


#2

I ran your code and it does't add up. .98 + 1.23 + 4.99 + (3 chocolate) 1.35 = 8.55


#3

Within your add function, lastTransactionAmount is not spelled correctly


#4

Thank you!
Now it's telling me the total is "NaN".... damn


#5

I don't think you defined that variable anywhere either. You're just assigning it. Try declaring it in the cashRegister just like total


#6

Thanks ! that helped... It's printing the wrong total now..


#7

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

Looks like in the last line here, you aren't changing the value of "this.total". First, I believe you need to use "this" on lastTransactionAmountYou. Also, you need to assign the value by either changing what you have to.

this.total -= this.lastTransactionAmount;

or

this.total = this.total - this.lastTransactionAmount

Both do the same thing but the first looks much nicer.


#8

Thanks guys, all your tips made it work. Just for reference the working code looks like this:

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(lastTransactionAmount){
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);


#9

You don't need to put a parameter for voidLastTransaction method since you are not putting any argument when you call it. I believe.


#10

How come this code is correct?

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

};

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);

Pleeze


#11

Sorry...

I mean why is this code correct?


#12

//Add the voidLastTransaction Method here
voidLastTransaction: function(lastTransactionAmount){
this.total -= this.lastTransactionAmount;

i think i should be:

//Add the voidLastTransaction Method here
voidLastTransaction: function(){
this.total -= this.lastTransactionAmount;


#13

I am having trouble too, "It says my bill is NaN"

var cashRegister = {
    total:0,
    //Dont forget to add your property
    add: function(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(lastTransactionAmount) {
        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
cashRegister.voidLastTransaction()
cashRegister.scan("chocolate",3);
//Show the total bill
console.log('Your bill is '+cashRegister.total);

#14

3 posts were split to a new topic: Bleep bleep


#18