Prime Numbers JavaScript

后端 未结 11 1127
长发绾君心
长发绾君心 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:26

    In your if statement you got

    if(n%i !==0 && n%2 !==0 && n%3 !== 0)
    

    you for loop is going till i >= 2, so the n%2 !== 0 is useless, when i = 2, your if would look like:

    if(n%2 !==0 && n%2 !==0 && n%3 !== 0)
    

    Thats 2x the same check, the same is for n%3, its already checked :).

    you should keep a bool to check the n%i !== 0, if it never reach this it's a prime.

    Good luck with your homework :).

提交回复
热议问题