Building a Cash Register! BLEEP BLEEP


#1



I keep getting an error that it is not the correct total.


Replace this line with your code.


#2

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

this.lastTransactionAmount holds 0 not the cost of last item.


#3

What @rcodeman is saying is that the above variable (as written) is a global, not an object attribute.


#4

So what am I supposed to do? I am lost...it is only subtracting one and then adding three.


#5

Consider,

var obj = {
    a: 0,
    f: function () {
        a = 42;
    }
}

When we run the method and then query obj.a, what will print?

obj.f();
console.log(obj.a);

?

0

console.log(a);

?

42

A variable inside a method needs context. After all, it is supposed to be an instance method so should be aware of its instance at time of invokation. The variable this refers to the present context, obj, in this instance.

var obj = {
    b: 0,
    f: function () {
        this.b = 42;
    }
}
obj.f();
console.log(obj.b));    // 42
console.log(b);         // raises an exception

#6

I forgot: this
when saving the value. Got it, thanks.


#7

You're welcome. I added another example above (and edited the typos (twice)).


#8

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