//Create a function that finds how many prime numbers there are, up to the given integer.
var numoffactor = 0
var count=0
function primenumber(str) {
for (var i = 0; i < str; i++)
for (var g = 0; g < i; g++) {
if (i % g == 0) //if a factor is found
{ numoffactor++ }
}
if (numoffactor == 0) { count++ }
return count
}
console.log(primenumber(9)) //showing 0 no matter what number I put in
console.log(numoffactor)//showing 12
我的逻辑是:从0到str,找到每个整数,每个i循环的因数,如果因数为零,则为质数。但是无论输入如何,输出都是9。而且许多因素似乎都没有了。
//Create a function that finds how many prime numbers there are, up to the given integer.
var numoffactor = 0
var count = 0
function primenumber(str) {
for (var i = 0; i < str; i++) {
for (var g = 0; g < i; g++) {
if (i % g == 0) //if a factor is found
{ numoffactor++ }
}
if (numoffactor == 0) { count++ }
}
return count
}
console.log(primenumber(9)) //showing 0 no matter what number I put in
console.log(numoffactor)//showing 14