Prime Number | JavaScript

preview_player
Показать описание
Prime Number - Let's solve this in a mathematical approach and programmatically using JavaScript.

Any number that is divisible by 1 and itself it's known as a Prime number, for example, 2, 3, 5, 7, 11, 13, and so on.

This video is part of the Protractor interview preparation, for more such videos, kindly go through the playlist.

Source code:

Download my XPath extension - LetXPath

Thanks for watching, if you like my videos, kindly subscribe and hit the bell notification 🔔 for instant updates.
Рекомендации по теме
Комментарии
Автор

At last! I found a simple and easy to understand code for prime number in Javascript :) Thank you so much, my friend :)

ajalimagno
Автор

Hi, your solution is correct. But we can optimise it further by running loop through the square root of the number. Start from 2 and run till the square root of the number. Rest code will be the same.

Codehunting
Автор

What will happen if we will replace i<n with i<=n
Reply me sir

raok
Автор

ye console.log() me aapke answer kese aa rahi h, , use ye extention chiye vscode k lie please bataiye

girishsoni
Автор

wht if nis 2, it is prime right but it will enter if statement

shubhamshetty
Автор

Hi I'm Mahendra Thank you for this video I have one question My question we have in javascript used `${n}` so the same like feature in python could tell me

mahendarmanda
Автор

bro i need to clarify something!! why have u written, return `${n} is a prime number ` particularly outside of the for loop?

niranjanravichandran
Автор

This program is not working correctly. Because returning at first divide process. If we enter 9, trying 9 divided by 2 and giving result. It's not even trying for each i values. So here a correct code, you guys can check it.

const isPrime = (n) => {
let count = 0
if(n<=2) {
return `${n} is not prime number`
}

for(let j = 2; j<n; j++) {
if(n%j==0) {
count++
}
}
if(count === 0) {
return `${n} is a prime number.`
}
else {
return `${n} is not prime number`
}
}


console.log(isPrime(9))

Ali-owwr
Автор

why do we need to put a loop if all we need is to check if the n % 2 === 0 or n % 3 === 0??
what's the purpose of using a loop in this?

basically all "not prime" numbers can be divided by 2 or 3,
since you've added a (n < 2) return is not a prime number,

what's the purpose of using a loop?
can't wrap my head around this?

why not just write:

function isPrime(n){
if (n<2)
return 'is not a prime number'

if (n % 2 === 0 || n % 3 === 0) {return 'is not a prime number'}

return 'is a prime number'
}

Thanks

momomeshtv
Автор

9 is not prime number but using this logic 9 is prime number

nareshmote