A
A
Andrey Dobrin2018-02-24 17:12:34
JavaScript
Andrey Dobrin, 2018-02-24 17:12:34

Please point out the error in this code. Where is she?

Can you please help me understand where is the error?

main: 
  for(var i = 2; i < 10; i++){
    
    for(var j = 2; j < i; j++){
      if(i % j == 0)
        continue main;
    }
    
    alert(i);
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-02-24
@andreydobrin

See how it works. The purpose of the algorithm is to display prime numbers in the range from 2 to 10. These are 2, 3, 5 and 7.
A prime number is a number that is only divisible by itself and one.
Therefore, for all cases when i % j == 0( j >=2 && j < ii is divided by j without a remainder for values ​​of j from 2 and higher, but less than i ) the number i is not simple and we need to exit the iteration of the cycle, but not the internal one in which we are currently , but external, since we are no longer interested in the current value of i . The main label is used for this . The call continue main;takes us to the next iteration of the outer loop.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question