0

I have two textboxes and one button. I putt numbers in the first textbox and press the button to add the number to a total which is displayed in the second box, till it reaches 1000. But the if statement doesn't work for some reason.

This works fine:

<html>
<title>Ask7</title>
<script>
var total=0;

function calculate()
{

    var box1;
    box1=parseFloat(document.getElementById("box1").value);
    total=total+box1;
    document.getElementById("box2").innerHTML="";
    document.getElementById("box2").value=total;


}
</script>
<body>

<h3>Give num:</h3>
<input id="box1" type="text"></input>
<button onclick="calculate()" type="button">ADD</button>
<br>

<h3>Total:</h3>
<input id="box2" readonly="readonly" type="text"></input>
</body>
</html>

This doesn't:

<html>
<title>Ask7</title>
<script>
var total=0;

function calculate()
{

if(total<1000)
{
    var box1;
    box1=parseFloat(document.getElementById("box1").value);
    total=total+box1;
    document.getElementById("box2").innerHTML="";
    document.getElementById("box2").value=total;
}
else
{
    alert("OVER 1000!");
    break;
}

}
</script>
<body>

<h3>Give num:</h3>
<input id="box1" type="text"></input>
<button onclick="calculate()" type="button">ADD</button>
<br>

<h3>Total:</h3>
<input id="box2" readonly="readonly" type="text"></input>
</body>
</html>

Basically I don't get why the if statement doesn't work.

2

2 Answers 2

1

Remove the break, it doesn't belong there.

I think you should have your code like this:

var total = 0;

function calculate() {
    var box1;
    box1 = parseFloat(document.getElementById("box1").value);
    total = total + box1;
    box2 = document.getElementById("box2");
    box2.value = total;

    if (total < 1000) {
    // do something
    } else {
        alert("OVER 1000!");
        // break;
        box2.value = 0; // to clean the value after 1000
    }
}

Demo

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

13 Comments

Still it doesnt get in the if statement.
Good catch, although I don't think this causes the problem because javascript will stop there, however the code before should be executed.
@user2874565: Don't edit your original question like this. Now it appears that the answer doesn't make sense since there is no break in your code. But that was the problem.
i removed the break on my code and run it in chrome but it displays the first time i press add but not if i press again...wth.
@user2874565, check my updated answer with demo, is that what you are looking for?
|
0
...
 else {
        alert("OVER 1000!");

        box2.value = 0; // to clean the value after 1000

        total = 0; // **** also reset the global var for reuse as still adding over 1000

}

2 Comments

In general, it's best to pull the explanation out and state in "regular speech" terms. Comments tend to hint at the idea, but rarely tell the full story.
i wanted to put as a comment in previous ans but i don't how, it wrking now :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.