0

I have been trying to make a javascript program to determine whether a given year is a leap year. The problem is that the prompt is not . if i remove the if-else statement then its working fine.

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Untitled Document</title>
</head>
<body>
  <button type="button" onClick="myfunc()"></button>
  <script>
    function myfunc() {
      var a = prompt("Enter the year?");
      if (a % 4 == 0) {
        if (a % 100 == 0) {
          if (a % 400 == 0) {
            window.alert("it is a leap year");
          }
          else:
            window.alert("it is not a leap year");
        }
        else:
          window.alert("it is a leap year");
      }
    }
  </script>
</body>
</html>
wazz
  • 4,953
  • 5
  • 20
  • 34
Sanjay Sajwan
  • 23
  • 1
  • 1
  • 3

3 Answers3

0

You are not doing good with if else all you need to do is proper curly braces closing. Just replace your script with this.You need to learn javascript if else here

    <script>
   function leapYear(year)
    {
       return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
    }
   function myfunc()
   {
     var a = prompt("Enter the year?");
     if(leapYear(a))
     {
      alert("Its a leap year");
     }
    else{
           alert("Not a leap year");
        }
    }
    </script>
TAHA SULTAN TEMURI
  • 4,031
  • 2
  • 40
  • 66
0

Your main issue is a syntax error - if you look in the browsers developer tools console, you should see an error message

SyntaxError: expected expression, got ':'

That's what firefox says, but other browsers may issue a different message, but the message should point to the line with the issue

The other issue, once you fix that, is that your code will alert for any year divisible by 4 only - if it's not divisible by 4, no alert happens at all

tidying your code, you'll see why

function myfunc() {
    var a = prompt("Enter the year?");

    if (a % 4 == 0) {
        if (a % 100 == 0) {
            if (a % 400 == 0) {
                window.alert("it is a leap year");
            } else {
                window.alert("it is not a leap year");
            }
        } else {
            window.alert("it is a leap year");
        }
    } // you would need an else here for a % 4 !== 0
}

So, your code would end up

function myfunc() {
    var a = prompt("Enter the year?");

    if (a % 4 == 0) {
        if (a % 100 == 0) {
            if (a % 400 == 0) {
                window.alert("it is a leap year");
            } else {
                window.alert("it is not a leap year");
            }
        } else {
            window.alert("it is a leap year");
        }
    } else {
        window.alert("it is a leap year");
    }
}

That's all very well, but you've got four alerts, when really you only need two

So, create another variable that will be true or false, depending on if the entered value is a leap year or not

function myfunc() {
    var a = prompt("Enter the year?");
    var isLeap = (a % 4 === 0) && ((a % 100 !== 0) || (a % 400 === 0));
    if (isLeap) {
        window.alert("it is a leap year");
    } else {
        window.alert("it is not a leap year");
    }
}
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
0

Prompt {syntax: result = window.prompt(message, default); } seems to work with HTML integration along with JS.

check this link for more

example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script src="YourJSFile.js"></script> //Use this and open in this html on a browser
</body>
</html>
Mohammad Yaser Ahmadi
  • 4,664
  • 3
  • 17
  • 39
bumba
  • 29
  • 1