0

Validation

Someone please explain this code. It find the list of prime numbers from 0 to max. I don't understand what !store[i] means and how if statement validates it since store is an empty array and the value of store[i] when i is an integer should be undefined because the array is empty. Secondly I don't understand the use of the bitwise operator << is the second loop. Thank you. function primeFactorsTo(max) { var store = [], i, j, primes = []; for (i = 2; i <= max; ++i) { if (!store[i]) { primes.push(i); for (j = i << 1; j <= max; j += i) { store[j] = true; } } } return primes; } console.log(primeFactorsTo(5)); // 2,3,5 console.log(primeFactorsTo(15)); // 2,3,5,7,11,13

17th Oct 2020, 2:14 PM
Logos
Logos - avatar
1 Resposta
+ 1
!store[i] initially store[0] it is undefined but in loop store[j] =true; adding boolean true When store[i] is false, and !store[i] becomes true and if executes... I<<1 is left shift operation.. Look at here.. https://www.sololearn.com/learn/4087/?ref=app
17th Oct 2020, 3:04 PM
Jayakrishna 🇮🇳