03/04/2021 - finding prime numbers
during a phone interview recently, i was how would i use javascript to find prime numbers below a a certain integer.
so far i have seldom touched javascript algorithms. only dabbled in python machine learning. but that is looking at other people's code.
i am feeling really insecure until i can solve the problem, even though the employer passed my feeble attempt.
here's the solution
See the Pen prime number algo by wly185 (@wly185) on CodePen.
- use a for loop, other methods like map and filter seems to crash
- use for .. of to iterate through the array values
- i added a break statement because i wanted to stop if the number was divisible and not search anymore, on to the next number
- filter out even numbers and number divisible by 5 (ends with 5 and zeros) to reduce the array length
- to generate an array, use array(integer) and fill(0) => an array with all zeros => map value to key index +1
Comments