javascript - Why is everything NOT prime? -



javascript - Why is everything NOT prime? -

i'm trying write programme find prime numbers. have basics of down, except no matter number set in, returns not prime. i've been messing way long , cannot figure out is. in "if" statement or isprime function? please help, , give thanks you!

var number = 0; function main() { number = parseint(prompt("please come in number determine whether or not prime:", "enter number")); while(isnan(number) === true) { alert("you entered invalid number. please reenter"); number = parseint(prompt("please come in number determine whether or not prime:", "enter number")); } isprime(number); if(prime = false) { alert("the number " + number + " prime number!"); } else { alert("the number " + number + " not prime number!"); } } /*------------------------------------------------------*/ function isprime(number) { if(number < 2) {return false;} var prime = false; for(var = 2; <= math.sqrt(number); i++) { if(number % == 0) {prime = true;} } homecoming prime; }

replace:

if(prime = false)

not because you're using assignment instead of comparison, because prime not defined (it's not going return/create global variable prime function call).

replace with:

if (!isprime(number)) {

now doesn't utilize variable store result of isprime call, uses phone call directly. if need result in more 1 place, thought assign variable:

var prime = isprime(number);

and rest of code:

if (prime == false)...

javascript

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -