Assessment wanted for Loops 3 skill tests

Hi, chris Please explain how iteration work I am not able to uderstand how looping work, In both ,when use isPrime(num) and when use while(!isPrime(i)) . I just want to know how iteration work I mean looping. how i increase and how decrease. for find prime. I hope you help me.

function isPrime(num) {
for(let i=2;i<num;i++) { // here how ‘i’ will work
if(num % i === 0) {
return false;
}
}
return true;
}

// Add your code here

while(i>=2) {
if(!isPrime(i)) { // and also how ‘i’ here is work
i–;
continue;
}

para.textContent += ${i} ;
i–;
}

Hello @alishamsher98

for(let i=2;i<num;i++)

  1. this line create variable called i and it’s value
    2)then the loop will test if i<num if true then it will make iterate one timewhich mean it do the loop body
    if(num % i === 0) {
    return false; // if num %2i will be 0 then this return line will return and exit the function which mean it will exit the loop also
  2. after one cycle of the loop i++ willl be excute and it increase i by 1
  3. the loop do the test part which is i<num if true it will continu
  4. it will keep repeat step 2 ,3,4 till the i<num turn to false and of course that the internal if statment does not exit the function the one i mentioned in step 2

if the loop end and normaly by i<num lead to false then the function return true

for the while loop

  1. i will be tested first if it >=2 or not if yes then the while body will be excuted if not then the while loop would work at all
  2. first it will check if i isprime by calling the isPrime function with i as parameter
  3. it will test if !isPrime(i) that mean the oposite of the return of the function
  4. if step 3 lead to true i will be decresed by 1
  5. continue mean it will go to the while test part which is i>=2 without continue the while body
    6)if step 3 lead to false it will do this para.textContent += ${i} ;
    then decrement i by 1

hope that help and have a nice day :slight_smile:
}
}
i++ will increament

1 Like

Thankyou… You really explain concept in very well.

you very welcome :slight_smile: