How to generate Prime Numbers in JavaScript?

JavascriptWeb DevelopmentFront End Technology

To generate prime numbers in JavaScript, you can try to run the following code

Example

Live Demo

<html>
   <head>
      <title>JavaScript Prime</title>
   </head>

   <body>
      <script>
         for (var limit = 1; limit <= 20; limit++) {
            var a = false;
            for (var i = 2; i <= limit; i++) {
               if (limit%i===0 &amp;&amp; i!==limit) {
                  a = true;
               }
            }
            if (a === false) {
               document.write("<br>"+limit);
            }
         }
      </script>
   </body>
</html>
raja
Published on 18-Jan-2018 13:10:41
Advertisements