function isPrimeNumber in javascript











up vote
0
down vote

favorite












I want to check if a number is a prime number by typing it in a box, then click on a button and get the value is prime number or is not a prime number. I have the following code, but for some reason I can't figure it out why it is not working - I get the value is prime number for any number I tried.






function isPrimeNumber() {
var n = document.getElementById("n").value;
for (var i = 2; i < n; i++) {
if (n % i === 0) {
document.getElementById("result").value = "not a prime number";
}
}
document.getElementById("result").value = "prime number";
}

<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>





Thanks in advance!










share|improve this question




















  • 1




    The assignment after the loop will happen regardless of the loop.
    – destoryer
    Nov 10 at 19:22















up vote
0
down vote

favorite












I want to check if a number is a prime number by typing it in a box, then click on a button and get the value is prime number or is not a prime number. I have the following code, but for some reason I can't figure it out why it is not working - I get the value is prime number for any number I tried.






function isPrimeNumber() {
var n = document.getElementById("n").value;
for (var i = 2; i < n; i++) {
if (n % i === 0) {
document.getElementById("result").value = "not a prime number";
}
}
document.getElementById("result").value = "prime number";
}

<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>





Thanks in advance!










share|improve this question




















  • 1




    The assignment after the loop will happen regardless of the loop.
    – destoryer
    Nov 10 at 19:22













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to check if a number is a prime number by typing it in a box, then click on a button and get the value is prime number or is not a prime number. I have the following code, but for some reason I can't figure it out why it is not working - I get the value is prime number for any number I tried.






function isPrimeNumber() {
var n = document.getElementById("n").value;
for (var i = 2; i < n; i++) {
if (n % i === 0) {
document.getElementById("result").value = "not a prime number";
}
}
document.getElementById("result").value = "prime number";
}

<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>





Thanks in advance!










share|improve this question















I want to check if a number is a prime number by typing it in a box, then click on a button and get the value is prime number or is not a prime number. I have the following code, but for some reason I can't figure it out why it is not working - I get the value is prime number for any number I tried.






function isPrimeNumber() {
var n = document.getElementById("n").value;
for (var i = 2; i < n; i++) {
if (n % i === 0) {
document.getElementById("result").value = "not a prime number";
}
}
document.getElementById("result").value = "prime number";
}

<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>





Thanks in advance!






function isPrimeNumber() {
var n = document.getElementById("n").value;
for (var i = 2; i < n; i++) {
if (n % i === 0) {
document.getElementById("result").value = "not a prime number";
}
}
document.getElementById("result").value = "prime number";
}

<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>





function isPrimeNumber() {
var n = document.getElementById("n").value;
for (var i = 2; i < n; i++) {
if (n % i === 0) {
document.getElementById("result").value = "not a prime number";
}
}
document.getElementById("result").value = "prime number";
}

<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 19:22









lumio

4,34122035




4,34122035










asked Nov 10 at 19:18









Ioana

112




112








  • 1




    The assignment after the loop will happen regardless of the loop.
    – destoryer
    Nov 10 at 19:22














  • 1




    The assignment after the loop will happen regardless of the loop.
    – destoryer
    Nov 10 at 19:22








1




1




The assignment after the loop will happen regardless of the loop.
– destoryer
Nov 10 at 19:22




The assignment after the loop will happen regardless of the loop.
– destoryer
Nov 10 at 19:22












3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










When you've found a prime number, you currently continue the loop. What you want to do is break it and/or return the result like so:






function isPrimeNumber() {
var n = document.getElementById("n").value;
for (var i = 2; i < n; i++) {
if (n % i === 0) {
document.getElementById("result").value = "not a prime number";
return true;
}
}
document.getElementById("result").value = "prime number";
return false;
}

<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>








share|improve this answer




























    up vote
    1
    down vote













    With document.getElementById("result").value="prime number"; you are always setting the result as prime number. You need to make it conditional. Try following






    function isPrimeNumber() {
    var n = document.getElementById("n").value;
    var isPrime = true;
    for (var i = 2; i < n; i++) {
    if (n % i === 0) {
    isPrime = false;
    break;
    }
    }
    document.getElementById("result").value = isPrime ? "prime number" : "not a prime number";
    }

    <form>
    <p>
    <input type="text" id="n">
    <input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
    <input type="text" id="result">
    </p>
    </form>








    share|improve this answer




























      up vote
      0
      down vote













      document.getElementById("result").value = "prime number";


      this statement executes irrespective of whether you have a prime number of not.



      Simplest solution would be to return from the function if if statement inside for loop executes



      if (n % i === 0) {
      document.getElementById("result").value = "not a prime number";
      return;
      }





      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














         

        draft saved


        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242557%2ffunction-isprimenumber-in-javascript%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        0
        down vote



        accepted










        When you've found a prime number, you currently continue the loop. What you want to do is break it and/or return the result like so:






        function isPrimeNumber() {
        var n = document.getElementById("n").value;
        for (var i = 2; i < n; i++) {
        if (n % i === 0) {
        document.getElementById("result").value = "not a prime number";
        return true;
        }
        }
        document.getElementById("result").value = "prime number";
        return false;
        }

        <form>
        <p>
        <input type="text" id="n">
        <input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
        <input type="text" id="result">
        </p>
        </form>








        share|improve this answer

























          up vote
          0
          down vote



          accepted










          When you've found a prime number, you currently continue the loop. What you want to do is break it and/or return the result like so:






          function isPrimeNumber() {
          var n = document.getElementById("n").value;
          for (var i = 2; i < n; i++) {
          if (n % i === 0) {
          document.getElementById("result").value = "not a prime number";
          return true;
          }
          }
          document.getElementById("result").value = "prime number";
          return false;
          }

          <form>
          <p>
          <input type="text" id="n">
          <input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
          <input type="text" id="result">
          </p>
          </form>








          share|improve this answer























            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            When you've found a prime number, you currently continue the loop. What you want to do is break it and/or return the result like so:






            function isPrimeNumber() {
            var n = document.getElementById("n").value;
            for (var i = 2; i < n; i++) {
            if (n % i === 0) {
            document.getElementById("result").value = "not a prime number";
            return true;
            }
            }
            document.getElementById("result").value = "prime number";
            return false;
            }

            <form>
            <p>
            <input type="text" id="n">
            <input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
            <input type="text" id="result">
            </p>
            </form>








            share|improve this answer












            When you've found a prime number, you currently continue the loop. What you want to do is break it and/or return the result like so:






            function isPrimeNumber() {
            var n = document.getElementById("n").value;
            for (var i = 2; i < n; i++) {
            if (n % i === 0) {
            document.getElementById("result").value = "not a prime number";
            return true;
            }
            }
            document.getElementById("result").value = "prime number";
            return false;
            }

            <form>
            <p>
            <input type="text" id="n">
            <input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
            <input type="text" id="result">
            </p>
            </form>








            function isPrimeNumber() {
            var n = document.getElementById("n").value;
            for (var i = 2; i < n; i++) {
            if (n % i === 0) {
            document.getElementById("result").value = "not a prime number";
            return true;
            }
            }
            document.getElementById("result").value = "prime number";
            return false;
            }

            <form>
            <p>
            <input type="text" id="n">
            <input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
            <input type="text" id="result">
            </p>
            </form>





            function isPrimeNumber() {
            var n = document.getElementById("n").value;
            for (var i = 2; i < n; i++) {
            if (n % i === 0) {
            document.getElementById("result").value = "not a prime number";
            return true;
            }
            }
            document.getElementById("result").value = "prime number";
            return false;
            }

            <form>
            <p>
            <input type="text" id="n">
            <input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
            <input type="text" id="result">
            </p>
            </form>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 10 at 19:23









            lumio

            4,34122035




            4,34122035
























                up vote
                1
                down vote













                With document.getElementById("result").value="prime number"; you are always setting the result as prime number. You need to make it conditional. Try following






                function isPrimeNumber() {
                var n = document.getElementById("n").value;
                var isPrime = true;
                for (var i = 2; i < n; i++) {
                if (n % i === 0) {
                isPrime = false;
                break;
                }
                }
                document.getElementById("result").value = isPrime ? "prime number" : "not a prime number";
                }

                <form>
                <p>
                <input type="text" id="n">
                <input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
                <input type="text" id="result">
                </p>
                </form>








                share|improve this answer

























                  up vote
                  1
                  down vote













                  With document.getElementById("result").value="prime number"; you are always setting the result as prime number. You need to make it conditional. Try following






                  function isPrimeNumber() {
                  var n = document.getElementById("n").value;
                  var isPrime = true;
                  for (var i = 2; i < n; i++) {
                  if (n % i === 0) {
                  isPrime = false;
                  break;
                  }
                  }
                  document.getElementById("result").value = isPrime ? "prime number" : "not a prime number";
                  }

                  <form>
                  <p>
                  <input type="text" id="n">
                  <input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
                  <input type="text" id="result">
                  </p>
                  </form>








                  share|improve this answer























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    With document.getElementById("result").value="prime number"; you are always setting the result as prime number. You need to make it conditional. Try following






                    function isPrimeNumber() {
                    var n = document.getElementById("n").value;
                    var isPrime = true;
                    for (var i = 2; i < n; i++) {
                    if (n % i === 0) {
                    isPrime = false;
                    break;
                    }
                    }
                    document.getElementById("result").value = isPrime ? "prime number" : "not a prime number";
                    }

                    <form>
                    <p>
                    <input type="text" id="n">
                    <input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
                    <input type="text" id="result">
                    </p>
                    </form>








                    share|improve this answer












                    With document.getElementById("result").value="prime number"; you are always setting the result as prime number. You need to make it conditional. Try following






                    function isPrimeNumber() {
                    var n = document.getElementById("n").value;
                    var isPrime = true;
                    for (var i = 2; i < n; i++) {
                    if (n % i === 0) {
                    isPrime = false;
                    break;
                    }
                    }
                    document.getElementById("result").value = isPrime ? "prime number" : "not a prime number";
                    }

                    <form>
                    <p>
                    <input type="text" id="n">
                    <input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
                    <input type="text" id="result">
                    </p>
                    </form>








                    function isPrimeNumber() {
                    var n = document.getElementById("n").value;
                    var isPrime = true;
                    for (var i = 2; i < n; i++) {
                    if (n % i === 0) {
                    isPrime = false;
                    break;
                    }
                    }
                    document.getElementById("result").value = isPrime ? "prime number" : "not a prime number";
                    }

                    <form>
                    <p>
                    <input type="text" id="n">
                    <input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
                    <input type="text" id="result">
                    </p>
                    </form>





                    function isPrimeNumber() {
                    var n = document.getElementById("n").value;
                    var isPrime = true;
                    for (var i = 2; i < n; i++) {
                    if (n % i === 0) {
                    isPrime = false;
                    break;
                    }
                    }
                    document.getElementById("result").value = isPrime ? "prime number" : "not a prime number";
                    }

                    <form>
                    <p>
                    <input type="text" id="n">
                    <input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
                    <input type="text" id="result">
                    </p>
                    </form>






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 10 at 19:22









                    Nikhil Aggarwal

                    23.1k32647




                    23.1k32647






















                        up vote
                        0
                        down vote













                        document.getElementById("result").value = "prime number";


                        this statement executes irrespective of whether you have a prime number of not.



                        Simplest solution would be to return from the function if if statement inside for loop executes



                        if (n % i === 0) {
                        document.getElementById("result").value = "not a prime number";
                        return;
                        }





                        share|improve this answer

























                          up vote
                          0
                          down vote













                          document.getElementById("result").value = "prime number";


                          this statement executes irrespective of whether you have a prime number of not.



                          Simplest solution would be to return from the function if if statement inside for loop executes



                          if (n % i === 0) {
                          document.getElementById("result").value = "not a prime number";
                          return;
                          }





                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            document.getElementById("result").value = "prime number";


                            this statement executes irrespective of whether you have a prime number of not.



                            Simplest solution would be to return from the function if if statement inside for loop executes



                            if (n % i === 0) {
                            document.getElementById("result").value = "not a prime number";
                            return;
                            }





                            share|improve this answer












                            document.getElementById("result").value = "prime number";


                            this statement executes irrespective of whether you have a prime number of not.



                            Simplest solution would be to return from the function if if statement inside for loop executes



                            if (n % i === 0) {
                            document.getElementById("result").value = "not a prime number";
                            return;
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 10 at 19:25









                            Yousaf

                            4,1252823




                            4,1252823






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242557%2ffunction-isprimenumber-in-javascript%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Bressuire

                                Vorschmack

                                Quarantine