Dimasub
Dimasub

Reputation: 13

JS loop inside a loop

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;
}

Upvotes: 1

Views: 187

Answers (5)

Aaron Plocharczyk
Aaron Plocharczyk

Reputation: 2842

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);
}

Upvotes: 1

Omri Attiya
Omri Attiya

Reputation: 4037

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;
}

Upvotes: 0

Pushprajsinh Chudasama
Pushprajsinh Chudasama

Reputation: 7949

  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

Upvotes: 0

T&#226;n
T&#226;n

Reputation: 1

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);
  }
}

Upvotes: 0

sachin_hg
sachin_hg

Reputation: 136

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);
}

Upvotes: 0

Related Questions