Bleep Bleep Help!


#1

I modified the code as follows, but it cannot run as well. I cannot figure out the problem, please help me !!!

By the way, this question gets me confused with the usage of "this".

Why does "this" apply to different cases in the question and change frequently ?

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.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('eggs', 3);

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


#2

What error message have you got? I've tried your code and it works. Result is:

Your bill is 10.14

#3

this keyword simply refers to the method within which it was used.


#4

The error is "Oops, try again. false"

I cannot figure out what wrong with my code as well.

Thanks


#5

I tried running your code and received the same error message. However, I found the problem. Look for a mistake here:

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

#6

What is the problem ? I cannot understand. Thx


#7

Take a closer look at the last transaction.


#8

I am sorry I cannot find the problem as well.

Did I type sth wrong ? I checked the code for 2 days =.=


#9

cashRegister.scan('chocolate',4);

This is the last transaction.

//Void the last transaction and then add 3 instead

This is the instruction.

cashRegister.scan('eggs', 3);

This is where you've made a mistake.


#10

I cannot find the problem as well.


#11

Come on :smiley:

Do these look equal to you?

'chocolate' === 'eggs' ?


#12

Anyway, I give up . Thx


#13

Dang, that's too bad.

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

Try 'chocolate' instead of 'eggs' .....


#14

Don't give up!
You have 4 transaction:

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',1);
cashRegister.scan('chocolate',4); // it's your last transaction

So instead of

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

should be

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

#15

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