lundi 8 janvier 2018

If statement in function issue

I can't get the if/else statement in the totalBalance function to work correctly in this code and I'm not sure why.

I have tried switching the greater than to less than and seeing if it solves the outcome but it makes no difference.

jsfiddle link

var moneyAmount = 0;
var food = 0;
var bills = 0;
var total = 0;

moneyAmount = prompt("how much money do you earn per month?");
amountCheck();

document.write("Your balance is " + "£" + moneyAmount + "<br>");

food = confirm("Do you have any food bills?");

if (food === true) {
  food = prompt("How much per week?")
  document.write("You spend £" + food + " on food per week <br>");
} else {
  alert("Lucky!")
};

totalBalance();

/* total = moneyAmount - food; */
console.log("money amount = " + moneyAmount);
console.log("food = " + food);
console.log("total = " + total);


function totalBalance() {
  total = moneyAmount - food;
  console.log("total is " + total);
  if (total > moneyAmount) {
    document.write("Your total amount of money per month is £" + total);
    console.log("nay");
  } else {
    document.write("You need to save more money £" + total);
    console.log("yay");
  };
}


function amountCheck() {
  while (isNaN(moneyAmount)) {
    alert("Please enter a numeric value");
    moneyAmount = prompt("how much money do you have to spend?");
  }
}

Aucun commentaire:

Enregistrer un commentaire