1

I've got an assignment to write a js code using only loops that recieves a number from a user and prints out all the prime numbers between 1 and that number.

Thats what I've done, but its not working as I expect it to, cant find what Im missing:

var num = parseInt(prompt('Please enter a number'));
var flag = 0;

for (var i=2 ; i<=num ; i++){
    for (var j=2 ; j<num ; j++){
        if (num%j==0){
            flag = 1;
            break;
        }       
    }
    if (flag==0) console.log(i);
    if (flag==1) flag=0;
}
FZs
  • 16,581
  • 13
  • 41
  • 50
Dimasub
  • 13
  • 5
  • 2
    Does this answer your question? [Number prime test in JavaScript](https://stackoverflow.com/questions/40200089/number-prime-test-in-javascript) – Tân Dec 28 '19 at 07:46
  • I need to output all of the primes in a certain range, not checking for a specific number if its a prime or not. – Dimasub Dec 28 '19 at 07:51
  • 1
    @Dimasub But how do you know that a number (that is part of the specified range) is a prime number? – Andreas Dec 28 '19 at 07:56

5 Answers5

1

When you're in your nested for loop, you're determining if i is a prime number, not num. So you want use the i variable there, like so:

var num = parseInt(prompt('Please enter a number'));
var flag = 0;

for (var i=2 ; i<=num ; i++){
    for (var j=2 ; j<i ; j++){
        if (i%j==0){
            flag = 1;
            break;
        }       
    }
    if (flag==0) console.log(i);
    if (flag==1) flag=0;
}

I've left the rest of your code like you had it so you understand what's going on as much as possible.


If you like short code, here's a short version:

var num = parseInt(prompt('Please enter a number'));
for(var i = 2, flag = 0; i <= num; i++, flag = 0){
  for(var j = 2; j < i; j++) flag = i % j == 0 ? 1 : flag;
  if(!flag) console.log(i);
}
Aaron Plocharczyk
  • 2,776
  • 2
  • 7
  • 15
0

You could try something like this:

var n = parseInt(prompt('Please enter a number'));
for(var i = 2; i <= n; i++){
    var flag = false;
    for(var j=2; j<i; j++){
        if(i%j == 0){
            flag = true;
            break
        }
    }
    if(!flag)console.log(i);
}

sachin_hg
  • 136
  • 3
0

I need to output all of the primes in a certain range, not checking for a specific number if its a prime or not.

You can try this:

function isPrime(num) {
  for(var i = 2; i < num; i++)
    if(num % i === 0) return false;
  return num > 1;
}

var num = parseInt(prompt('Please enter a number'));

for (var i = 2; i <= num; i++) {
  if (isPrime(i)) {
    console.log(i);
  }
}
Tân
  • 1
  • 15
  • 56
  • 102
0

Lets understand what you did wrong in your code:

  1. In the inner loop you run until num. Why? You want all the numbers from 2 to num that are primes, how do you check if a number i is a prime? You try to divide i with all the numbers from 2 to i-1, that is why the inner loop should be from j=2 until j<i.
  2. In the inner loop when you calc num % j == 0 you just ask if num is a prime or not, while you should ask whether i is a prime, because i is between 2 and num, which is the range where you want to find primes.

var num = parseInt(prompt('Please enter a number'));
var flag = 0;

for (var i = 2; i <= num; i++) {
  for (var j = 2; j < i; j++) {
    if (i % j == 0) {
      flag = 1;
      break;
    }
  }
  if (flag == 0) console.log(i);
  if (flag == 1) flag = 0;
}
Omri Attiya
  • 3,917
  • 3
  • 19
  • 35
0

  var flag = false;
      var person = prompt("Please enter your name");
      console.log(person);
      for(var i = 2 ; i < person ; i++){
        for(var j = 2 ; j< i ; j++){
          if(i % j == 0){
            flag = true;
          }
        }
        if(flag == false){
          console.log(i , "is prome");
        }
        flag = false;
      }

Try out this code . I will work perfect

Pushprajsinh Chudasama
  • 7,772
  • 4
  • 20
  • 43