Friday, June 4, 2021

Javascript Practice 70: JS Hero - break and continue

 
Question: 

Write a function isPrime that checks whether a passed number is prime. In case of a prime number it should return true, otherwise false.

Example: isPrime(7) should return true and isPrime(8) should return false.




Answer: 

function isPrime (n) {

if (n <= 1) return false; 

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

if (n % i == 0) return true; 

return false; 

};





> a prime number is a natural number
> it is greater than 1
> it is only divisible by 1 and itself; eg 7 
> so  (7 % 1 = 0) & (7 % 7 = 0)
> but 7 % any other number is not 0; meaning it will always have a remainder
> this is because 7 is not divisible by any other number
> 1 is not a prime number 
> to be a prime number; the condition is to be divisible by any 2 positive divisors
> 7 is divisible by 7 & 1; 
> 3 is divisible by 3 & 1
> since 1 is only divisible by 1 and no other number, it does not fulfil the condition
> ∴ 1 is not a prime number
> we start by declaring a function isPrime
> it has 1 parameter ' n '
> we open an 'if' statement to check if the number 'n' provided is less than or equal to 1
> like this n <= 1
> it is supposed to check if the number is less than 1; prime nos are greater than 1
> it is also supposed to check if the number is equal to 1; 1 is not a prime number
> if the condition is true; then our function will return false and terminate the function
>> if it is not true then we proceed further down towards another check in the form of a loop
>> we open a 'for' statement
>> a 'for' statement creates a loop 
>> this loop consists of 3 optional expressions followed by a block statement
>> block statement is executed in the 'for' loop 
>> 3 optional expressions are - initialization, condition & final expression
>>> initialization: we declare a variable only once before the loop begins
>>>> we initialize the 'for' loop with the number 2
>>>> this is because we already checked if the number is 1 or less than 1
>>>> it has gone past that condition to check the number
>>>> if the number is not 1 or less than 1, then it has to be more than 1
>>>> which is why we start the loop with number 2
>>> condition: this condition is evaluated before each loop iteration
>>>> if the condition is true then block statement is executed
>>>> if the condition is false then for loop is exited without execution to the next line of code
>>>> our number (n) has to be between 2 & n
>>>> we express our condition like this: i < n; we know that i = 2 already
>>> final expression: this is evaluated at the end of each loop iteration
>>>> executed every time after the code block has been executed
>>>> it increases / decreases the value each time the code block has been executed
>>>> like this i++ / i--
>>>> this final expression will keep increasing or decreasing and is based on the condition
>>>> if the condition is met then it will stop incrementing or decrementing
>>>> it only works till the condition is not met
>>>> our condition is i < n
> for eg '' for (let i = 2; i < 5; i++)
> our loop will start at 2
> it will check if 2 is less than 5
> it will execute the code in the block below it
> it will then increase the variable 'i' by 1
> it will again check if i + 1 < 5 and if it is, then it will execute the code in the block below again
> after the execution of the code in the block below, it will again increment the value of i by 1
> this will keep happening as long as i < 5
> this is why it is called as a 'loop' 
> once i = 5, the loop will stop and proceed to the next code below the for loop
>>> after the construction of the for loop and its three expressions we open a statement
>>>> this statement is to be executed as long as the condition in the loop is true
>>>> we open an 'if' statement
>>>> our condition is to check if the 'n' divided by i, has a remainder of 0
>>>> like this: (n % 1 == 0)
>>>> if it does have a remainder of 0, then our condition is true
>>>> if our condition is evaluated as true then we return false and stop the execution
>>>> if our condition is evaluated as false then it re-iterates back with an incremented number
>>>> this keeps on happening till 'i = n'
>>>> the loop will then stop functioning and return 'true'

or

> to check if a number is prime, first we need to check if it is not 1
> after we have checked that, then we need to check if this number is divisible by any other number except 1 and itself; i.e. 2 divisors
> if it is divisible by any other number apart from the 2 divisors then it is not a prime number
> our code should reflect the above 2 points
> we check if the number is 1; if its not then we proceed with other code
> to check the number of divisors a particular 'number n' has, we simply divide it by all the numbers below it and check the remainder of that division
> we do that by creating a for loop with an 'if' condition
> this loop will divide the number by all the numbers below it and check the remainder 
> if the remainder is 0 then our number is not prime
> if the remainder is not 0, then our loop will keep executing till it stops

1 comment:

  1. function isPrime (n) {


    if (n <= 1) return false; 


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


    if (n % i == 0) return false; 


    return true; 


    };

    ReplyDelete