How to write prime number program with jQuery + javascript + HTML

Views: 8   

Demo

Code

<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $("#btn").click(function() {
                var number = $("#num").val();
                output = "";
                if (number == 1 || number == 2) {
                    $("#result").html("it's  a prime number");
                    return;
                }

                for (i = 2; i <= number - 1; i++) {
                    if (number % i == 0) {
                        output = "it's not a prime number";
                        break;
                    } else {
                        output = "it's  a prime number";
                    }
                }
                $("#result").html(output);
            });
        });
    </script>
</head>

<body>
    Enter a number:
    <input type="text" name="value" id="num"></input>
    <button id="btn"> Submit </button>
    <h4>Output: </h4>
    <span id="result"></span>
</body>

</html>
On By