Find Next Prime Number Please!

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!

<button type="button" onclick="five()">Next</button>
<p id="five">1</p>


function five() {
              var isPrime = false;
                var num = document.getElementById("five");
                if(num.innerHTML === "1") {
                num.innerHTML = "3";
              } else {

              function findNextPrime(prime) {
                var counter = 0;
                isPrime = false;

                while(!isPrime) {
                  if(prime % 2 === 0) {
                    console.log("not prime " + prime);
                for(var i=2; i<prime; i++) {
                  if(prime % i === 0) {
                    return isPrime = false;
                  } else {
                  if(counter === (prime - 2)){
                    num.innerHTML = prime;
                    return isPrime = true;

What is contained in the element with id ‘five’? As far as I can see, this can only work once, because it will take that value, log the answer, then it’s still the same value, so you can only ever get the same answer

I haven’t looked at all of your code but, in addition to what @DanCouper pointed out, there is also an issue in the for loop in your isPrime() function:


for (var i = 3; i < n; n + 2) {
  // ...

I suspect you actually want i = i + 2 instead of n + 2.

Yeah I got both of those taken care of, and I’ve done a bit of reorganizing. My problem now is that it updates until I get to the number 8? I’m trying to work through why that would be…