10_ for 練習 _ is Prime Number ?

  • <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8" />
            <title>Is Prime Number?</title>
        </head>
        <body>
            <script type="text/javascript">
                var xNumber = +prompt("請輸入一個大於1的數字:");
                var isPrimeNumber = true;
                
                if(xNumber>1 && xNumber != NaN)
                {
                    for(i=2; i<xNumber; i++)
                    {
                       if(xNumber%i==0)
                       {
                           isPrimeNumber = false;
                       }
                    }
                }
                if(isPrimeNumber)
                {
                    document.write(xNumber+" is a Prime Number.");
                }
                else
                {
                    document.write(xNumber+" is NOT a Prime Number.");
                }
            </script>
        </body>
    </html>
相關文章
相關標籤/搜索