How to find prime numbers in arrays by JavaScript
This article introduces the knowledge about "how JavaScript finds prime numbers in arrays". In the actual operation process of cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
Operating environment of this tutorial: Windows 7 system, Javascript version 1.8.5, Dell G3 computer.
What are prime numbers?
Prime numbers, also known as prime numbers, refer to natural numbers greater than 1 that have no other factors except 1 and itself.
JavaScript How to find prime numbers in an array
Use the for loop statement or filter() method to loop through the array, in each loop to determine whether an array element is prime, if yes, then output the element.
The method of determining whether it is a prime number is to divide a number by 2 to sqrt(this number). If it can be divided exactly, it indicates that the number is not a prime number, and vice versa.
Here's how to do it:
Using the for loop
var a = [31,33,35,37,39,41,43,45,57,49,51,53];for(var i=0;i