JS loop inside a loop

javascript nested for loop array
for loop javascript
print 1 to 10 numbers in javascript using for loop
javascript loop through array of objects
javascript for loop with index
how to do a nested for loop in javascript
loop through string javascript
javascript repeat 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;
}

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

For loop inside For Loop Javascript, Loops are used in JavaScript to perform repeated tasks based on a of getting an object's keys using an expression within the for-in loop's� JavaScript supports different kinds of loops: for - loops through a block of code a number of times. for/in - loops through the properties of an object. for/of - loops through the values of an iterable object. while - loops through a block of code while a specified condition is true.

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

Loops and iteration, its " Did you write an if/else statement inside the second for loop?" here's the code : var myPlaces = ['UAE ' , 'Singapore' , 'Hyderabad'];. var� Javascript supports the nested loop in javascript. The loop can have one or more or simple can have any number of loops defined inside another loop, and also can behave n level of nesting inside the loop. The nested loop is also called as inner loop and the loop in which the nested loop defined is an outer loop.

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

JavaScript Loops Explained: For Loop, While Loop, Dowhile Loop , JavaScript Loops. Loops are handy JavaScript supports different kinds of loops : for - loops If you omit statement 2, you must provide a break inside the loop. The statement inside the loop is executed only when the condition evaluates to true. The loop is terminated if the condition evaluates to false. Note that the condition is optional. If you omit it, the for loop statement considers it as true. 3) post-expression. The for loop statement also evaluates the post-expression after each loop

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

Javascript, 'for loops inside for loops ', JavaScript supports different kinds of loops: for - loops through a Also, if you omit this parameter, you must provide a break inside the loop. Otherwise the loop � The answer to your question is that since the while loop has no brackets to indicate the code inside the loop. only one line, the hash += "#"; gets execute, and when you get to the line that modifies the value of the count: count += n;, the while loop wont repeat since you already exited that loop.

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

5. For loop inside of for loop - JavaScript, 2) condition. The condition is an expression that is evaluated once before every iteration. The statement inside the loop is executed only when the� JavaScript async and await in loops 1st May 2019. Basic async and await is simple. Things get a bit more complicated when you try to use await in loops.. In this article, I want to share some gotchas to watch out for if you intend to use await in loops.

JavaScript for Loop, In the syntax above there are three expressions inside the for statement: the initialization, the condition, and the final expression, also known as� In this article, we will see how to write a promise function inside a loop. Promises inside a loop - Javascript ES6. If you want to learn more about Javascript. check out this articles. Prototypal Inheritance - Javascript Weekly. Understanding Closures in Javascript - Javascript Weekly. Configuring Babel for Node/Express

JavaScript for Statement, The loop in JavaScript and many other languages are similar in syntax and functionality to C / C++. Working with loops is quite straightforward, just set the index, match the iteration condition and finally increment the index to make sure it is exhaustive at any iteration that occurs for which the match condition is false.

JavaScript for Loop By Examples, How to loop in a React component JSX Published Nov 06, 2018 If you have a set of elements you need to loop upon to generate a JSX partial, you can create a loop, and then add JSX to an array:

Comments
  • Does this answer your question? Number prime test in JavaScript
  • 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 But how do you know that a number (that is part of the specified range) is a prime number?
  • The first answer that explains what the problem is. Thank you!
  • Why do you add an answer when you've voted to close this question as duplicate? o.O
  • @Andreas Because of the comment which I'd quoted in the answer.
  • Either it's a dupe or not. Vote to close or add an answer.
  • @Andreas I write this answer just to provide information to OP how to do that. Not for reputation something like that. It's also because OP doesn't understand why it's duplicated.
  • @Andreas So, should I delete it?