//标记法 function fn(m, n) { var count = ""; for (i = m; i <= n; i++) {//第一次循环 var flage = true;//设一个标记 for (j = 2; j < i; j++) { if (i % j === 0) {//第二次循环 flage = false;//不满足条件改变标记, break;//跳出循环 } } if (flage) {//满足条件,也就是为true时 count += i + "," } } return count } console.log(fn(100, 200)) //调用函数,输出值
//计数法 function fn(m, n) { var sun = ""; for (i = m; i <= n; i++) {//第一次循环 var count = 0;//初值为零 for (j = 2; j < i; j++) {//第二次循环 if (i % j === 0) {//不满足条件加1 count+=1 } } if (count === 0) { sun += i + "," } } return sun } console.log(fn(100, 200))