-2

I am trying to make a javaScript function to check the prime number. It's perfectly working for lot of all the numbers I've tested but only comes wrong output when the value is 9. Could you please anyone check below code and advise?

const primeDetector = function (inputNumber) {
  if (inputNumber < 1) {
    console.log("Your input is not a prime");
  }
  if (inputNumber === 1) {
    console.log("Your input is Neither prime nor composite");
  }
  if (inputNumber === 2) {
    console.log(`${inputNumber} is a prime number`);
  } else if (inputNumber > 2) {
    for (var i = 2; i < inputNumber; i++) {
      if (inputNumber % i == 0) {
        console.log(`${inputNumber} is NOT a prime number`);
        break;
      } else {
        console.log(`${inputNumber} is a PRIME number`);
        break;
      }
    }
  }
};
primeDetector();
  • 3
    [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – VLAZ Aug 25 '22 at 07:19
  • It's not *the* problem, but you probably want `else` on the end of those first two `if`s (and then you don't need the `if` part of the final `else if` at all; you've eliminated all the other options -- except NaN, which you might want to allow for, perhaps in the same branch that checks for `=== 1`). Separately: Why is the call at the end not passing *anything* to `primeDetector`? – T.J. Crowder Aug 25 '22 at 07:25
  • *"...but only comes wrong output when the value is 9"* What output do you get? What output do you expect? – T.J. Crowder Aug 25 '22 at 07:26
  • @T.J.Crowder I am expecting "9 is not a prime number" but it comes "9 is a prime number", every other number working fine. – Abir Tasrif Anto Aug 25 '22 at 07:33
  • 1
    Your code produces wrong output for all odd non-prime numbers, e.g. 15, 21, 25, 27. The code only checks whether the number is even or odd. – jabaa Aug 25 '22 at 07:43

3 Answers3

0

As far as I can tell your function only checks if a number is even or odd, because you break the loop instantly. You are just checking i=2.

Jannis
  • 1
  • No, I am putting value in the primeDetector(); For any other value, my output is fine, problem occurs only if i put 9. Ex- primeDetector(9); – Abir Tasrif Anto Aug 25 '22 at 07:34
  • I get various wrong outputs using your code starting with 15. You are just lucky because until 15 every odd number is a prime. Print out `i` in the loop and see that it never actually passes 2. You are breaking out of it always in the first iteration. – Jannis Aug 25 '22 at 07:51
  • You are right, thanks for your valuable time. – Abir Tasrif Anto Aug 25 '22 at 07:58
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 29 '22 at 15:00
0

The problem is that you are forgetting that you are doing your check in a loop. You treat it as if it is not in a loop:

for (var i = 2; i < inputNumber; i++) {
  if (inputNumber % i == 0) {
    console.log(`${inputNumber} is NOT a prime number`);
    break;
  } else {
    console.log(`${inputNumber} is maybe a PRIME number, if I don't say it is not a prime number at the end then it is PRIME, right now I'm not sure yet`);
    // break; // <-- this break is the bug
  }
}

Remember, this is a loop. If the number is not divisible by i then it does NOT mean it is a prime but it just means you have not tested with all the values of i yet. You still need to complete the loop to test all the values of i.

If you want to only print one message when it is a prime you need to remember if you have passed the test or not. In programming, remembering means using memory and using memory means using a variable:

var is_prime = true;

for (var i = 2; i < inputNumber; i++) {
  if (inputNumber % i == 0) {
    console.log(`${inputNumber} is NOT a prime number`);
    is_prime = false;
    break;
  }
}

// We only know if the number is a prime HERE
// not inside the loop above

if (is_prime) {
    console.log(`${inputNumber} is PRIME`);
}
slebetman
  • 109,858
  • 19
  • 140
  • 171
  • I have removed that break, works fine but when I put 9, I am getting two outputs `9 is a PRIME number` & `9 is NOT a prime number` – Abir Tasrif Anto Aug 25 '22 at 07:40
  • Remember it is a loop. Check my console log. I modified it to **not** say it is a PRIME but instead to say "I'm not sure it is a prime yet, wait until the end of this program". Remember, first you are testing `9 % 2` which is not zero but that does not mean it is a prime, next you are testing `9 % 3` which is zero – slebetman Aug 25 '22 at 07:42
0

The logic of the loop is very simple. Iterate from i = 2 until i = inputNumber - 1, see if inputNumber is divisible by i. If yes, definitely not a prime number. If no, keep going until we are sure it is not divisible by ANY of i in that range.

const primeDetector = function (inputNumber) {
  let isPrime = true;

  if (inputNumber < 1) {
    console.log("Your input is not a prime");
    isPrime = false;
  }
  if (inputNumber === 1) {
    console.log("Your input is Neither prime nor composite");
    isPrime = false;
  }
  
  if (inputNumber === 2) {
    console.log(`${inputNumber} is a prime number`);
    isPrime = true;
  } else if (inputNumber > 2) {
  
    for (var i = 2; i < inputNumber; i++) {
      console.log(`Checking if ${inputNumber} is divisible by ${i}`);
      if (inputNumber % i == 0) {
        console.log(`Opps. ${inputNumber} is divisible by ${i}. ${inputNumber} is then NOT a prime number`);
        isPrime = false;
        break; // Found one divisible, definitely not a prime number. No need to continue, just exit the loop
      } else {
        console.log(`${inputNumber} is not divisible by ${i}. Maybe a prime number. Wail till end ya`);
        // break;
      }
    }
    
    isPrime? console.log(`${inputNumber} is a prime number`) : console.log(`${inputNumber} is not a prime number`);
  }
};

primeDetector(9);
primeDetector(11);
geckob
  • 7,680
  • 5
  • 30
  • 39