In the code below I created a constructor function. I would like to add the value called total of two objects (one is called candy and the other is called computer). They obviously won’t add…they concatenate. However, when I perform multiplication it multiplies as expected. What do I need to do to perform addition to them ?
Code is below.For my example I use multiplication and I commented the troubled area.
<!DOCTYPE html>
<meta charset="UTF-8">
<title>checkoutTemplate</title>
<script>
function Item (item,price,tax,addedFee,total){
this.item = item;
this.price = price;
this.tax = function () {
if (typeof tax == 'undefined')
{
tax = price * .10;
}
else {
tax = 0;
}
return tax;
};
this.addedFee = function () {
if (typeof addedFee == 'undefined')
{
addedFee = price * .05;
}
else {
addedFee = 0;
}
return addedFee;
};
this.total = function () {return this.price+ this.tax()+ this.addedFee()};
};
var computer = new Item("computer", 1000,"NA","NA");
var candy = new Item("candy", 0.50,"NA","NA");
document.write( "the total output value: " + candy.total() * computer.total() ); // Troubled area. I want to add candy.total and computer.total.Not multiply.
Thank you very much
Use
()to let number plus perform first: