0

I'm new to HTML and I am trying to make a postage calculator using HTML and Javascript, the user inputs name weight of the package if they want express delivery. All the options affect the price but for some reason my variable express cost is stuck at £5.

<!DOCTYPE html>
<html>
<head>
<title>Postage Calculator</title>
 <script>
    function calCost() {
        console.log("Invoked calCost()")
        document.getElementById('displayName').innerHTML = document.getElementById("name").value;
        var expressCost = 0;
        if (document.getElementById('express').checked) ; //always checked 
        {
            console.log("yup"); // this was to test if it was staying checked which it does.
            expressCost = 5;
        }
        if (document.getElementById('r1').checked) {
            document.getElementById('displayCost').innerHTML = "\u00A3" + (2 * document.getElementById("weight").value + expressCost);
        }
        else if (document.getElementById('r2').checked) {
            document.getElementById('displayCost').innerHTML = "\u00A3" + (10 * document.getElementById("weight").value + expressCost);
        }

        else {
            document.getElementById('displayCost').innerHTML = "\u00A3" + (5 * document.getElementById("weight").value + expressCost);
        }
    }
</script>


<label> Enter your name <input type = "text" name="name" id="name"></label><br>
 <br>
 <label> Enter weight  <input type = "text" name="weight" id ="weight"></label> Kilograms<br>
 <br>
 <label>Do you want express delivery?<input type="checkbox" name="express" id="express"></label> Additional 5 Pounds <br>
 <br>
 <input type="radio" id="r1" name="rate" value="United Kingdom" checked="checked"> United Kingdom - 2 per Kilo<br>
 <input type="radio" id="r2" name="rate" value="United States"> United States - 10 per Kilo<br>
 <input type="radio" id="r3" name="rate" value="Europe">  Europe - 5 per Kilo <br>

This is the javascript for working out the total cost, i've tested starting up the program and submitting and instantly £5 is added to total even without it being checked and I have no idea.

Also just noticed that my variable is "variable initializer is redundant"

2
  • 6
    can you add all your HTML? Commented Dec 13, 2018 at 12:30
  • As Nick says, please create a minimal reproducible example. Edit: yeah, just remove that semi-colon. Commented Dec 13, 2018 at 12:31

1 Answer 1

7
if (document.getElementById('express').checked) ; //always checked 

That line is bugging you out. It contains ;, which creates a block scope with the next { } that is just simply always executed.

edit: Problems like this can be avoided using a javascript linter and be consistent with your style and indenting.

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

2 Comments

That seems to have fixed it but I dont really understand how that semi colon caused such a error, Do I not always need to finish each line with a semi colon like in java?
I wrote the same kind of answer with some explanations there this was for C language, but it does the same in most languages (including javascript)

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.