0

I wrote a small javascript code, but it has a problem, the page can not show anything. I think the problem is in the line "else if..." or "else...", because if I comment these two lines, the code runs without any problem

<html>
<head>
    <script language="javascript">
        var var1 = window.prompt("please input");
        var var2 = window.prompt("please input2");
        var1 = parseFloat(var1);
        var2 = parseFloat(var2);

        if (var1< var2) {document.writeln("the second number is bigger")};
        else if (var1> var2) {document.writeln("the first number is bigger")};
        else {document.writeln("They are the same")};
    </script>
</head>
<body>
</body>
</html>
5
  • 12
    Remove the ; from the closing braces of your if statements. Commented Dec 25, 2015 at 18:58
  • Take a look at this jsfiddle jsfiddle.net/www139/c0fj5bmg Commented Dec 25, 2015 at 18:59
  • 2
    Welcome to Stackoverflow. Merry Christmas and hope you are successful at learning JavaScript. Commented Dec 25, 2015 at 18:59
  • 1
    Also, Always check the console i.imgur.com/nv2MHFp.png Commented Dec 25, 2015 at 19:02
  • 1
    @www139 Thanks! i am so careless. Merry Christmas! Commented Dec 25, 2015 at 19:02

5 Answers 5

3

Your javascript should be like this

var var1 = window.prompt("please input");
var var2 = window.prompt("please input2");
var1 = parseFloat(var1);
var2 = parseFloat(var2);

if (var1 < var2) {
  document.writeln("the second number is bigger");
} else if (var1 > var2) {
  document.writeln("the first number is bigger");
} else {
  document.writeln("They are the same");
}

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

2 Comments

I beat you to an answer earlier today, also, Praveen ;D
@PhiterFernandes Merry Christmas! LoL.
1

Try removing the semicolon, ;, from after the brackets of your if statements:

    if (var1< var2) {document.writeln("the second number is bigger")}
    else if (var1> var2) {document.writeln("the first number is bigger")}
    else {document.writeln("They are the same")}

Have a look at this SO answer: https://stackoverflow.com/a/17036218/4206206

Essestially, a semicolon isn't used to end a group of statements, but rather to end a single statement.


A point with your code, if you're using HTML5 you don't need the language="javascript" in your script tags:

<script language="javascript">

Can become simply

<script>

Comments

1

Should be:

<html>
<head>
    <script language="javascript">
        var var1 = window.prompt("please input");
        var var2 = window.prompt("please input2");
        var1 = parseFloat(var1);
        var2 = parseFloat(var2);

        if (var1 < var2) {
            document.writeln("the second number is bigger");
        }
        else if (var1 > var2) {
            document.writeln("the first number is bigger");
        }
        else {
            document.writeln("They are the same");
        }
    </script>
</head>
<body>
</body>
</html>

your semi colons were wrong

Comments

0
if (var1< var2) {document.writeln(" ... ")};
                                           ^

Your semicolons at the end of your if, else if, and else blocks are your issue. They are prematurely ending your if blocks.

Comments

-1

Just make sure you parseFloat before you compare, because it just compares as string, and remember, 10 comes before 2, so by string 10 < 2! And you don't need the ; at the end:

var var1 = window.prompt("please input");
var var2 = window.prompt("please input2");
var1 = parseFloat(var1);
var2 = parseFloat(var2);

if (var1 < var2) {
  document.writeln("the second number is bigger");
} else if (var1 > var2) {
  document.writeln("the first number is bigger");
} else {
  document.writeln("They are the same");
}

Comments

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.