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>
Phiter
  • 14,570
  • 14
  • 50
  • 84
dsfg
  • 29
  • 1
  • 5

5 Answers5

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");
}
rishabh dev
  • 1,711
  • 1
  • 15
  • 26
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

J0B
  • 1,648
  • 1
  • 12
  • 24
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>
Community
  • 1
  • 1
joe_young
  • 4,107
  • 2
  • 27
  • 38
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.

Nick Zuber
  • 5,467
  • 3
  • 24
  • 48
-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");
}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252