2/7. Manually Add It Up?


#1

I might of misread something, but I don't understand how to call the method four times. Can someone help me by giving the correct code? Here is my code now..

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

//call the add method for our items
cashRegister.add(this.total);

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

Would I have to call that multiple times? I'm confused


#2

This is an early lesson that demonstrates what the program will be doing later on, programmatically, rather than manually. For now, though, just call the add() method with four different amounts:

cashRegister.add(2.99);
cashRegister.add(1.99);
cashRegister.add(2.49);
cashRegister.add(0.98);

Then log the total:

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

That's if I am reading this question right. If I am not on base, please post a link so we can check things out for outselves.

Off topic best practice tip:

Let readers see the operators by leaving white space around them.

    a + b
    // rather than
    a+b

You'll thank yourself later for adopting this practice on all operators.


#3

Oh..Okay. That makes a lot more sense...Thank you!


#4

seemed like an array made more sense.
var x = [prices]
var z = 0;
for (var i = 0; i < x.length; i++){
z += x[i];
};

then the method is the object.method(z) or is this just more convoluted than necessary?


#5

I was lazy lol..

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