Prime Numbers JavaScript

后端 未结 11 1153
长发绾君心
长发绾君心 2020-12-08 11:47

Can someone please give me guidance on getting the primenumbers here? This is homework so I don\'t want the answer but some pointers would be greatly appreciated. It\'s real

11条回答
  •  长情又很酷
    2020-12-08 12:42

    There is a function that will return true if the number is prime and false if it is not:

    function isPrime(x){     
          d = x-1;
          while (d > 1){
            if ((x % d) == 0) return false;
            d--;
          }
          return true;
        }
    

    Checkout the demo: http://jsbin.com/velapabedi/1/

    
    
    
      
      JS Bin
      
      
    
    
    
    
    

提交回复
热议问题