Okay first the definition of a prime number we are using: A prime number n is a number such that the numbers 2 to n - 1 are not factors of n. This means that when we divide n by any number between 2 and n - 1, the remainder will NOT be 0.
So first, we create a for loop from number1 to number2. We will check if each number in this range is a prime:
for (let i = number1; i <= number2; i++) {
Now, we actually have to check if the number, i, is a prime, using our method of checking to see if numbers from 2 to i - 1 are NOT factors of i. We can use another for loop for this, with a variable j that goes from 2 to i - 1:
for (let i = number1; i <= number2; i++) {
let flag = 0;
for (let j = 2; j < i; j++) {
Inside our j loop, we check if j is a factor of i. Remember, if the number is prime, no j will be a factor of i, so the remainder of i divided by j will not be 0. If i % j is 0, then we know i is not prime, so we exit out of the for loop and change flag to 1, so we know not to output it:
for (let i = number1; i <= number2; i++) {
let flag = 0;
for (let j = 2; j < i; j++) {
if (i % j == 0) {
flag = 1;
break;
}
}
If the for loop completed without break, which means that no j was a factor of i, we know that i is prime, which means flag is still 0. This means if flag is 0, we can print out i, since we know it is prime:
for (let i = number1; i <= number2; i++) {
let flag = 0;
for (let j = 2; j < i; j++) {
if (i % j == 0) {
flag = 1;
break;
}
}
if (i > 1 && flag == 0) console.log(i);
}
Then, i iterates to the next number and the cycle continues until the loop and code is finished.
Please let me know if you need any further help or clarification! :)