How to Find Prime Numbers in Array by JavaScript?

Hello Friends, Today I will tell you through my tutorial how you can get prime numbers from array integers value numbers through javascript. Today I will tell you step to step through this tutorial.

Friends, the language of javascript is very important for a developers and designer. If you do not know the language of javascript then it can not be a good developer or designer. How can prime numbers be obtained from the array via javascript. So let’s try to understand.

get prime numbers from array using js, fetch prime numbers in array by javascript, extract prime numbers from array using javascript

We will take a variable of javascript for examples. And we will take the integer value from 2 to leke 20 in the array in that variable. Now we will extract the prime numbers from the value of this array by writing the code of javascript. Its code is explained to you below. You can test by looking and writing this code in your file.

<script>
var primenumArray = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
primenumArray = primenumArray.filter((number) => {
for (var i = 2; i <= Math.sqrt(number); i++) {
if (number % i === 0) return false;
}
return true;
});
// console.log(primenumArray);
document.getElementById('message').innerHTML = primenumArray;
</script>

Now I will tell you how to write the code of this javascript and then how to test this file. You simply create a file named simple prime-number.html and then save it in any of your folders. Then take the file open on your browser.

prime-number.html

<!DOCTYPE html>
<html>
<head>
<title>Find Prime Numbers in Array by JavaScript</title>
</head>
<body>
<h1>Find Prime Numbers in Array by JavaScript</h1>
<b>Find Prime Numbers in Array Format :-</b><p id="message"></p>
</body>
<script>
var primenumArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
primenumArray = primenumArray.filter((number) => {
for (var i = 2; i <= Math.sqrt(number); i++) {
if (number % i === 0) return false;
}
return true;
});
// console.log(primenumArray);
document.getElementById('message').innerHTML = primenumArray;
</script>
</html>