Reputation: 601
I've been trying to pass a challenge for so long, and despite reading up multiple solutions and advice online I can't seem to apply it correctly.
The instructions are "Define a function isPrime that takes one integer argument and returns true or false depending on if the integer is a prime."
I've tried a lot of things but this is the furthest I've been able to get:
function isPrime(num) {
if (num <= 1) return false;
if (num === 2) return true;
for (var i = 2; i < num; i++)
if (num % i === 0) return false;
else return true;
}
But then I'm told: "9 is not a prime number."
Any help would be appreciated and thanks!
Upvotes: 4
Views: 10609
Reputation: 1360
I have try to calculate using RxJS operators. I hope you have enjoyed.
import { range, filter, take } from 'rxjs';
/**
* It returns true if the number is prime, false otherwise
* @param {number} x - number - the number to check if it's prime
* @returns A function that takes a number and returns a boolean.
*/
const isPrime = (x: number): boolean => {
let state = true;
range(2, x - 2 > 0 ? x - 2 : 0)
.pipe(
filter((loop: number): boolean => x % loop === 0),
take(1),
)
.subscribe(() => {
state = false;
});
return x > 1 && state;
};
const theNumber = 44;
console.log(theNumber, isPrime(theNumber) ? ' is' : ' is not', ' prime number');
Bonus:
Below code gives the prime numbers between 5 and 100. You can also subscribe to obs[1] for rest numbers.
const start = 5;
const end = 100;
const obs = partition(
generate(
start,
(x: number) => x < end,
(x: number) => x + 1,
),
(x) => isPrime(x),
);
obs[0].pipe(toArray()).subscribe((x) => console.log('Prime numbers: ', x));
obs[1].pipe(toArray()).subscribe((x) => console.log('Rest numbers: ', x));
Upvotes: 0
Reputation: 113
function primeNo(){
//"number1" is "ID" of HTML input block.
var num2 = document.getElementById("number1").value;
var dev = num2 / 2;
var i;
for(i=2; i < dev ; i++){
if(num2%i == 0){
//"ans1" is "ID of <p> where ans needs to display"
document.getElementById("ans1").innerHTML = "Not a Prime No";
break;
}
else{
document.getElementById("ans1").innerHTML = "Prime No";
}
}
}
Upvotes: 0
Reputation: 115282
You should avoid the else case and return only after for loop completion. Although for loop count can be reduced by updating condition to i <= Math.sqrt(num)
( as @PatrickRoberts suggested ).
function isPrime(num) {
if (num <= 1) return false;
if (num === 2) return true;
// storing the calculated value would be much
// better than calculating in each iteration
var sqrt = Math.sqrt(num);
for (var i = 2; i <= sqrt; i++)
if (num % i === 0) return false;
return true;
}
FYI : In your code on the first iteration of the loop num % i === 0
(9 % 2
) would be false
and it would return the else statement(true
).
Upvotes: 4
Reputation: 24822
The problem is with the else return true;
in your for loop.
At the first iteration, where i
is 2, just after you checked that 9 % i
wasn't 0, you execute the else
block and return true
.
You shouldn't return true
before all the iterations failed to return false
.
Upvotes: 4