Posts

Showing posts from November 16, 2018

function isPrimeNumber in javascript

Image
up vote 0 down vote favorite I want to check if a number is a prime number by typing it in a box, then click on a button and get the value is prime number or is not a prime number. I have the following code, but for some reason I can't figure it out why it is not working - I get the value is prime number for any number I tried. function isPrimeNumber() { var n = document.getElementById("n").value; for (var i = 2; i < n; i++) { if (n % i === 0) { document.getElementById("result").value = "not a prime number"; } } document.getElementById("result").value = "prime number"; } <form> <p> <input type="text" id="n"> <input type="button" value="Check if I am prime!&quo