Prime Numbers Till n

PHOTO EMBED

Thu Nov 05 2020 12:01:57 GMT+0000 (UTC)

Saved by @snigdhakhurana10

function printPrime(n){
	let factors = 1, iterator = 1, primeArr = [];
    while(primeArr.length < n){
    	for(let i=2; i<=iterator; i++){
        	if(iterator % i === 0)
            	factors++;
        }
        if(iterator === 1)
        	primeArr.push(iterator);
        if(factors === 2){
        	primeArr.push(iterator);
        }
        factors = 1;
        iterator++;
    }
    return primeArr;
}

console.log(printPrime(10));
content_copyCOPY