+ 2
Issues regarding prime numbers(Please help!!)
I was having some trouble listing out prime numbers by using the .map() method in javascript. What I was trying to achieve is from 1 to x, whereas x is an integer input by user as a max value, list every integer from 1 to x and then make the factors of the numbers in an array form. For instance, from 1 to 5, the expected result should be: [[1],[1,2],[1,,3][1,2,4],[1,5]] and ill just make the result showing the length of each of the subarray instead: [1,2,2,3,2] make if statements inside for loops and check if the value is equal to 2, then print it. (They should be prime numbers right?)(In this case:2,3,5) Is this possible? I just can't find out the problem here. Or it was because of my misunderstanding towards the .map() method?
1 Resposta
+ 2
The code i typed is as below:
function primenumberFinder(max)
{
var numArr = [];
var primeArr = [];
for(var num = 1 ; num<=max ; num++)
{
numArr.push(num);
}
console.log (numArr);
numArr.map(function (x){
var facArr = [];
for(var i=1 ; i<=x ; i++)
{
if(Number.isInteger(x/i)===true)
{
facArr.push(i);
}
}
return facArr.length;
}
)
for(var j = 0; j<=numArr.length ; j++)
{
if(numArr[j]===2)
{
primeArr.push(numArr[j]);
}
}
console.log(primeArr)
}
primenumberFinder(15);