-1

I'm trying to code a script to test whether a user-inputted number is prime or not. I'm coding several different primality tests, but one in particular is giving me a hard time.

function isPrimeSix() {
    var numberPrimeSix = document.getElementById("primeSixInput").value;
    var loopCount = 0;
    for (var i = 1; i < Math.floor((numberPrimeSix / 6) + 1) + 1; i++)
    {
        if (numberPrimeSix === (6 * i) + 1)
        {
            //Irrelevant code here//
        }
        else if (numberPrimeSix === (6 * i) - 1)
        {
            //More irrelevant code//
        }
        else
        {
            loopCount++
        }
    };
    if (numberPrimeSix === 2 || numberPrimeSix === 3 || numberPrimeSix === 5 || numberPrimeSix === 7)
    {
        alert(numberPrimeSix + " is prime.");
    }
    else if (prime === false || loopCount === Math.floor((numberPrimeSix / 6) + 1))
    {
        alert(numberPrimeSix + " is not prime.");
    }
    else if (prime === true)
    {
        alert(numberPrimeSix + " is prime.");
    }
    else
    {
        alert("Error");
    };
}

Every time the for loop goes around, the embedded if statement will not evaluate, even if for that particular value of i one of the statements is true. Regardless of what number is assigned to numberPrimeSix, the script will always go to the else section of the loop, meaning that an alert will pop up telling me that the number is not prime (because the value of loopCount is equal to the value defined by the last if statement).

Can anyone tell me why this is? I hope this makes sense, and if the 'irrelevant code' is needed I'll provide it. Thanks!

osjjames
  • 25
  • 1
  • 2
  • 8
  • 2
    a string will never be exactly equal (===) to a number. (finding this dupe will be near impossible...) – Kevin B Jul 06 '15 at 18:08
  • How do you know that that's what it's doing? The JavaScript runtime doesn't just randomly choose not to execute statements. Have you stepped through the code in a debugger? – Pointy Jul 06 '15 at 18:09
  • To expand on @KevinB s comment, try doing `numberPrimeSix = parseInt(numberPrimeSix, 10);` – Mike Cluck Jul 06 '15 at 18:10
  • further reading: http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons you should definitely continue to use `===` since it is faster and you're in a loop, however, that means you'll need to do the type conversion before the loop. – Kevin B Jul 06 '15 at 18:19
  • Kevin B was right, I changed all the `===` to `==` and it works fine. I'm very new to this language and had no idea there were different ways to say 'equal to'. Thanks for your help. – osjjames Jul 06 '15 at 18:20

1 Answers1

0

A string will never be exactly equal (===) to a number. In JavaScript, if you use ==, the operands will be converted to be the same type before comparing. If you instead use ===, that step will be skipped and instead they will be tested for strict equality, meaning same type and value, not just same value.

You have two options. Replace === with ==, or, convert the value before the loop and continue using strict equal. It will be much faster to convert the value before the loop so that you're not converting it over and over with each iteration.

var numberPrimeSix = parseInt(document.getElementById("primeSixInput").value, 10);

more reading: Which equals operator (== vs ===) should be used in JavaScript comparisons?

Community
  • 1
  • 1
Kevin B
  • 94,570
  • 16
  • 163
  • 180