2/7 Cash register


#1


What is wrong with my code

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


//call the add method for our items
var itemCost = {
    Egg:0.98,
    Milk:0.23,
    Magazine:4.99,
    Chocolate:0.45
};

for(var i in itemCost){
    cashRegister.add(itemCost[i]);
}
//Show the total bill
console.log('Your bill is '+cashRegister.total);


#2

Please link from lesson.


#3

@vuk81 Code you provided here is clean and you'll get the output without any error as well. However when they asked you to call add method and add the values manually in to the array, you wrote the following instead.

//call the add method for our items
var itemCost = {
    Egg:0.98,
    Milk:0.23,
    Magazine:4.99,
    Chocolate:0.45
};

All you have to do is get rid of above code and for loop and just add the item's cost manually using add method like this

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

//call the add method for our items
cashRegister.add(0.98);
cashRegister.add(1.23);
cashRegister.add(4.99);
cashRegister.add(0.45);

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

#4

Dear netsolver,
thanks for your help.

Best regards


#5

No worries at all mate :slight_smile:


#6

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