function isPrimeNumber in javascript
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!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
Thanks in advance!
javascript
add a comment |
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!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
Thanks in advance!
javascript
1
The assignment after the loop will happen regardless of the loop.
– destoryer
Nov 10 at 19:22
add a comment |
up vote
0
down vote
favorite
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!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
Thanks in advance!
javascript
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!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
Thanks in advance!
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!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
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!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
javascript
javascript
edited Nov 10 at 19:22
lumio
4,34122035
4,34122035
asked Nov 10 at 19:18
Ioana
112
112
1
The assignment after the loop will happen regardless of the loop.
– destoryer
Nov 10 at 19:22
add a comment |
1
The assignment after the loop will happen regardless of the loop.
– destoryer
Nov 10 at 19:22
1
1
The assignment after the loop will happen regardless of the loop.
– destoryer
Nov 10 at 19:22
The assignment after the loop will happen regardless of the loop.
– destoryer
Nov 10 at 19:22
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
When you've found a prime number, you currently continue the loop. What you want to do is break it and/or return the result like so:
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";
return true;
}
}
document.getElementById("result").value = "prime number";
return false;
}
<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
add a comment |
up vote
1
down vote
With document.getElementById("result").value="prime number";
you are always setting the result as prime number
. You need to make it conditional. Try following
function isPrimeNumber() {
var n = document.getElementById("n").value;
var isPrime = true;
for (var i = 2; i < n; i++) {
if (n % i === 0) {
isPrime = false;
break;
}
}
document.getElementById("result").value = isPrime ? "prime number" : "not a prime number";
}
<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
add a comment |
up vote
0
down vote
document.getElementById("result").value = "prime number";
this statement executes irrespective of whether you have a prime number of not.
Simplest solution would be to return from the function if if statement
inside for loop
executes
if (n % i === 0) {
document.getElementById("result").value = "not a prime number";
return;
}
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
When you've found a prime number, you currently continue the loop. What you want to do is break it and/or return the result like so:
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";
return true;
}
}
document.getElementById("result").value = "prime number";
return false;
}
<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
add a comment |
up vote
0
down vote
accepted
When you've found a prime number, you currently continue the loop. What you want to do is break it and/or return the result like so:
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";
return true;
}
}
document.getElementById("result").value = "prime number";
return false;
}
<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
When you've found a prime number, you currently continue the loop. What you want to do is break it and/or return the result like so:
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";
return true;
}
}
document.getElementById("result").value = "prime number";
return false;
}
<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
When you've found a prime number, you currently continue the loop. What you want to do is break it and/or return the result like so:
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";
return true;
}
}
document.getElementById("result").value = "prime number";
return false;
}
<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
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";
return true;
}
}
document.getElementById("result").value = "prime number";
return false;
}
<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
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";
return true;
}
}
document.getElementById("result").value = "prime number";
return false;
}
<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
answered Nov 10 at 19:23
lumio
4,34122035
4,34122035
add a comment |
add a comment |
up vote
1
down vote
With document.getElementById("result").value="prime number";
you are always setting the result as prime number
. You need to make it conditional. Try following
function isPrimeNumber() {
var n = document.getElementById("n").value;
var isPrime = true;
for (var i = 2; i < n; i++) {
if (n % i === 0) {
isPrime = false;
break;
}
}
document.getElementById("result").value = isPrime ? "prime number" : "not a prime number";
}
<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
add a comment |
up vote
1
down vote
With document.getElementById("result").value="prime number";
you are always setting the result as prime number
. You need to make it conditional. Try following
function isPrimeNumber() {
var n = document.getElementById("n").value;
var isPrime = true;
for (var i = 2; i < n; i++) {
if (n % i === 0) {
isPrime = false;
break;
}
}
document.getElementById("result").value = isPrime ? "prime number" : "not a prime number";
}
<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
add a comment |
up vote
1
down vote
up vote
1
down vote
With document.getElementById("result").value="prime number";
you are always setting the result as prime number
. You need to make it conditional. Try following
function isPrimeNumber() {
var n = document.getElementById("n").value;
var isPrime = true;
for (var i = 2; i < n; i++) {
if (n % i === 0) {
isPrime = false;
break;
}
}
document.getElementById("result").value = isPrime ? "prime number" : "not a prime number";
}
<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
With document.getElementById("result").value="prime number";
you are always setting the result as prime number
. You need to make it conditional. Try following
function isPrimeNumber() {
var n = document.getElementById("n").value;
var isPrime = true;
for (var i = 2; i < n; i++) {
if (n % i === 0) {
isPrime = false;
break;
}
}
document.getElementById("result").value = isPrime ? "prime number" : "not a prime number";
}
<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
function isPrimeNumber() {
var n = document.getElementById("n").value;
var isPrime = true;
for (var i = 2; i < n; i++) {
if (n % i === 0) {
isPrime = false;
break;
}
}
document.getElementById("result").value = isPrime ? "prime number" : "not a prime number";
}
<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
function isPrimeNumber() {
var n = document.getElementById("n").value;
var isPrime = true;
for (var i = 2; i < n; i++) {
if (n % i === 0) {
isPrime = false;
break;
}
}
document.getElementById("result").value = isPrime ? "prime number" : "not a prime number";
}
<form>
<p>
<input type="text" id="n">
<input type="button" value="Check if I am prime!" onclick="isPrimeNumber();">
<input type="text" id="result">
</p>
</form>
answered Nov 10 at 19:22
Nikhil Aggarwal
23.1k32647
23.1k32647
add a comment |
add a comment |
up vote
0
down vote
document.getElementById("result").value = "prime number";
this statement executes irrespective of whether you have a prime number of not.
Simplest solution would be to return from the function if if statement
inside for loop
executes
if (n % i === 0) {
document.getElementById("result").value = "not a prime number";
return;
}
add a comment |
up vote
0
down vote
document.getElementById("result").value = "prime number";
this statement executes irrespective of whether you have a prime number of not.
Simplest solution would be to return from the function if if statement
inside for loop
executes
if (n % i === 0) {
document.getElementById("result").value = "not a prime number";
return;
}
add a comment |
up vote
0
down vote
up vote
0
down vote
document.getElementById("result").value = "prime number";
this statement executes irrespective of whether you have a prime number of not.
Simplest solution would be to return from the function if if statement
inside for loop
executes
if (n % i === 0) {
document.getElementById("result").value = "not a prime number";
return;
}
document.getElementById("result").value = "prime number";
this statement executes irrespective of whether you have a prime number of not.
Simplest solution would be to return from the function if if statement
inside for loop
executes
if (n % i === 0) {
document.getElementById("result").value = "not a prime number";
return;
}
answered Nov 10 at 19:25
Yousaf
4,1252823
4,1252823
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242557%2ffunction-isprimenumber-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
The assignment after the loop will happen regardless of the loop.
– destoryer
Nov 10 at 19:22