Adding one variable to another variable

I am a student and am struggling to get my javascript to add VAT to another variable.

can someone help - display shows 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;
}

      

+3


source to share


1 answer


Move var vat = total*0.14;

to the end of the function. You have to calculate chanu after calculating the sum.



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;
}

      

+4


source







All Articles