+ 1

1

8. Write a JavaScript function that accepts a number as a parameter and check the number is prime or not. Note : A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

12th Dec 2016, 6:46 PM
Nashwan Aziz
Nashwan Aziz - avatar
2 Respostas
+ 5
One inefficient way of doing this would be (in pseudocode, sorry): IsPrime(x) { var y = 2; while (y < x) { if (x % y == 0) { write("x is not prime"); return; // exit method } y++; } write("x is prime"); } you could also add a check at the start of the method to make sure x > 1.
13th Dec 2016, 12:41 AM
Jafca
Jafca - avatar
+ 1
8. Write a JavaScript function that accepts a number as a parameter and check the number is prime or not. Note : A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. One inefficient way of doing this would be (in pseudocode, sorry): IsPrime(x) { var y = 2; while (y < x) { if (x % y == 0) { write("x is not prime"); return; // exit method } y++; } write("x is prime"); } you could also add a check at the start of the method to make sure x > 1.
13th Dec 2016, 1:39 PM
Nashwan Aziz
Nashwan Aziz - avatar