Ok so this is driving me crazy and I’ve been working on it for days. Someone please help, my goal is to make a program that generates the next prime number, and I’m getting stuck at the first check, my isPrime function checks and consoles the first few numbers, up until 8 but then i can’t move on, i want to be able to move in succession with the onclick function, but I’m sure there’s a problem with my JavaScript in finding the next prime number. Thanks!
HTML:
<button type="button" onclick="five()">Next</button>
<p id="five">1</p>
JavaScript:
function five() {
var isPrime = false;
var num = document.getElementById("five");
if(num.innerHTML === "1") {
num.innerHTML = "3";
console.log(num.innerHTML);
} else {
findNextPrime(parseInt(num.innerHTML));
}
function findNextPrime(prime) {
var counter = 0;
prime++;
isPrime = false;
while(!isPrime) {
if(prime % 2 === 0) {
console.log("not prime " + prime);
prime++;
}
for(var i=2; i<prime; i++) {
if(prime % i === 0) {
return isPrime = false;
} else {
counter++;
}
if(counter === (prime - 2)){
console.log(prime);
num.innerHTML = prime;
return isPrime = true;
}
}
}
prime++;
}
}
}```