dsfg
dsfg

Reputation: 29

Else if and Else in JavaScript

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>

Upvotes: 0

Views: 135

Answers (5)

J0B
J0B

Reputation: 1648

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

Upvotes: 1

joe_young
joe_young

Reputation: 4125

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>

Upvotes: 1

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167162

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");
}

Upvotes: -1

Nick Zuber
Nick Zuber

Reputation: 5637

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.

Upvotes: 0

rishabh dev
rishabh dev

Reputation: 1743

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");
}

Upvotes: 3

Related Questions