I'm really new to coding, and recently started learning JavaScript.
I'm trying to get my head around if-else statements, and tried to put together an extremely basic budgeting function that subtracts budget from cost. It's only showing the else result though, and doesn't seem to care what I put in the inputs.
let budget = parseFloat(document.getElementById("budget").value);
let cost = parseFloat(document.getElementById("cost").value);
function shoppingCalculator(){
let x = budget - cost;
if (x >= 0){
alert("You are in budget!")
} else {
alert("You are not in budget!")
}
}
<html>
<head>
<title>
JS for Dummies
</title>
</head>
<body>
<div>
<form action="">
<input type="number" id="budget" placeholder="Budget" style="width: 100px;">
<br>
<input type="number" id="cost" placeholder="Cost" style="width: 100px;">
<br>
<button type="button" onclick="shoppingCalculator()"></button>
</form>
</div>
</body>
<script src="jsnew.js"></script>
</html>
Sorry for the dumb question. Thanks for all your help :)
Aucun commentaire:
Enregistrer un commentaire