-1

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 :)

2
  • What value are you getting on the x variable? Commented Dec 16, 2020 at 21:13
  • You read the values when the page loads. The values do not keep updating.... Commented Dec 16, 2020 at 21:13

1 Answer 1

4

I think the only problem here is that you need to get the values inside the function, not outside

function shoppingCalculator(){
    let budget = parseFloat(document.getElementById("budget").value);
    let cost = parseFloat(document.getElementById("cost").value);

    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()">Calculate</button>
            </form>
        </div>

    </body>

    <script src="jsnew.js"></script>
</html>

Sign up to request clarification or add additional context in comments.

2 Comments

It worked! Thanks so much. Can I ask why it's important that the values are placed inside the function? I thought that variables were applicable to functions even if created outside of them. Again, thanks!
@nope It is important because you want to get the value at the time the function runs. If they are outside, the value is whatever it is when the page was built in the browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.