# How the cycle works with the internal loop to print Prime numbers?

Started learning JavaScript in the tutorial https://learn.javascript.ru
I got such a task :

A natural number greater than 1 is called Prime if it is anything not divisible, except themselves and 1.
In other words, n>1 is Prime if when divided by any number from 2 to n-1 is the remainder.
Create code that prints all Prime numbers from range from 2 to 10. The result should be: 2,3,5,7.
PS the Code should be easily modified for any other intervals.

This is the solution:
``````nextPrime:
for (var i = 2; i < 10; i++) {

for (var j = 2; j < i; j++) {
if (i % j == 0) continue nextPrime;
}

Help to understand how it works. I tried to disassemble all the action and in the end I got what do all the numbers from 2 - 10 must be displayed))) but it is not happening. Tell me where I made a mistake and is not properly understood.

How I reasoned:
1) Start the outer loop
a)first iteration: i = 2, the condition i < 10
b)continue to climb in the inner loop. the first iteration in the inner loop: j = 2, the condition j < i (2 < 2) is not executed.
C)exit the inner loop in action j++ == (3)
d)the action to be executed i++ == (3)

2)Run a second iteration of the outer loop:
a) i = 3, the condition i < 10
b) continue to climb in the inner loop. the first iteration in the inner loop: j = 3, the condition j < i (3 < 3) is not executed.
C)exit the inner loop in action j++ == (4)
d)the action to be executed i++ == (4)

3) Launching the third iteration of the outer loop:
a) i = 4, the condition i < 10
b) continue to climb in the inner loop.
the first iteration in the inner loop: j = 4, the condition j < i (4 < 4) is not performed.
C)exit the inner loop in action j++ == (5)
g)execute alert(4)....(although it does not appear=)))

And so on))) by my calculation if condition did not when I work, that is not what I understand the cycles explain right now going crazy over it)
July 2nd 19 at 13:47
July 2nd 19 at 13:49
Solution
1)as soon As the loop exits, all data is available only in him, and have no external links - disappear. Accordingly, each entry in the inner loop you will have a new variable j = 2.
2)In the case j < i (2 < 2) j++ implementation will not come.
1)the Second paragraph I understood j++ will not come.
2) about the first item if everything happens as you say then the condition if(i%j == 0)
never be executed because i is always increasing and j = 2, hence again, all numbers must be displayed alert(i) - Randal.Reynol commented on July 2nd 19 at 13:52
j = 2 at each ENTRANCE! in the inner loop. When i = 3 the inner loop body will be executed 1 times and j will be equal to 3, the execution of the inner loop will be interrupted, because 3 < 3 == false and at the next sign in the inner loop(when i = 4) execution will start again with j = 2. - alden32 commented on July 2nd 19 at 13:55
All I caught )) the Solution to your
I could barely understand a scheme of only two levels of the cycle and after painted all in stages. And during the development it will need to describe the horror) - Randal.Reynol commented on July 2nd 19 at 13:58
July 2nd 19 at 13:51
"the first iteration in the inner loop: j = 4, the condition j < i (4 < 4) is not running."
No, the first iteration j = 2.
Then if this reasoning: j =2, the condition j - Randal.Reynol commented on July 2nd 19 at 13:54

Find more questions by tags JavaScript