2: Mannually add up. Console shows wrong. Why?


#1


Code passes the lesson ok, but the console is saying the amount is 0. Just wondering why it is doing that, and if something is wrong with my code.


var cashRegister = {
    total:0,
    add: function(itemCost){
        this.total += itemCost;
    }
};


//call the add method for our items


//Show the total bill
console.log('Your bill is '+cashRegister.total);
cashRegister.add(0.98);
cashRegister.add(1.23);
cashRegister.add(4.99);
cashRegister.add(0.45);


#2

Initially, the total value is equal to 0.

After execution of the add method, this value will be changed.

Move console.log expression below the add method calls to get the correct output :slight_smile:

console.log('Your bill is '+cashRegister.total);
// ==> Your bill is 0

cashRegister.add(0.98);
cashRegister.add(1.23);
cashRegister.add(4.99);
cashRegister.add(0.45);

console.log('Your bill is '+cashRegister.total);
// ==> Your bill is 7.65

#3

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