-2

Trying to calculate this but not working for me. Is there any issue with the daya variable? The whole code -

function change_payment() {
  var amount = parseFloat($("#amount").val());
  var plan = $("#select-payment").val();
  var percent = 5.7;
  var daya = 5;
  if(plan == "1")
  percent = 2.5;
  daya = 5;
  else if(plan == "2")
  percent = 3;
  daya = 6;
  console.log(amount);
  var daily = ((amount/100)*percent).toFixed(1);
  var total = (daily*daya).toFixed(1);
  $("#daily").html(daily);
  $("#total").html(total);
}
soywod
  • 4,377
  • 3
  • 26
  • 47
BightJay
  • 1
  • 1
  • 1
    Use brackets for your if statements https://stackoverflow.com/questions/4797286/are-braces-necessary-in-one-line-statements-in-javascript – MartW Dec 23 '19 at 17:27
  • 1
    Does this answer your question? [Are braces necessary in one-line statements in JavaScript?](https://stackoverflow.com/questions/4797286/are-braces-necessary-in-one-line-statements-in-javascript) – MartW Dec 23 '19 at 17:28

1 Answers1

0

You miss brackets in your statement.

if (plan == "1")
percent = 2.5;
daya = 5;

is not equal to:

if (plan == "1") {
  percent = 2.5;
  daya = 5;
}

but is equal to:

if (plan == "1") {
  percent = 2.5;
}

daya = 5;

Have a look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else.

soywod
  • 4,377
  • 3
  • 26
  • 47