Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding one variable to another variable [closed]

I'm a student and struggling to get my javascript to add the VAT to the other 'variable'

can somebody please assist - the vat displays as "0"

function calculateTotal(){
  var total = 0;
  var vat = total*0.14;

  for (var k = 0; k < document.forms.service.length; k++)
  {    
    if(document.forms.service.elements[k].checked){
      total+=Number(document.forms.service.elements[k].value);
    }
  }

  document.getElementById("total").innerHTML= "The total is R " + total;
  document.getElementById("vat").innerHTML= "The total VAT is R " + vat;
}
like image 890
Yolanda Ferreira Avatar asked Aug 23 '26 05:08

Yolanda Ferreira


1 Answers

Move var vat = total*0.14; to the end of the function. You have to calculate the vat after you calculate the total.

function calculateTotal(){
  var total = 0;

  for (var k = 0; k < document.forms.service.length; k++)
  {    
    if(document.forms.service.elements[k].checked){
      total+=Number(document.forms.service.elements[k].value);
    }
  }

  var vat = total*0.14;
  document.getElementById("total").innerHTML= "The total is R " + total;
  document.getElementById("vat").innerHTML= "The total VAT is R " + vat;
}
like image 138
Juan Mendes Avatar answered Aug 24 '26 19:08

Juan Mendes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!