使用 forEach 循环在 Javascript 中生成 1 到 50 之间的质数

这是我的代码,但我的答案不是我想要的..


请检查此并给我一个使用 Foreach 循环 b/w 1-50 获取素数的解决方案


提前致谢 :)


function isPrime(num) {

    for ( var i = 2; i < num; i++ ) {

        if ( num % i === 0 ) {

            return false;

        }

    }

    return true;

}

    var txt = "";

    function shown(n) {

        var arr = [2];

        arr.forEach(myFunction);


document.getElementById("foreach").innerHTML = txt;

// document.getElementById('forLoop').innerHTML = arr; // use arr result on your own

}

function myFunction(arr, index, array) {


var i;

var arr = [2];


if ( isPrime(i) ) {

    arr.push(i);

      }

  txt += arr + "<br>"; 

}


shown(50);


森栏
浏览 128回答 4
4回答

大话西游666

对于这个级别的作业来说,这可能是一个太高级的答案,但从技术上讲,它遵循规则(使用 Array.forEach)并且有效。primes()基于先前的素数生成新的素数。所以不会测试所有整数的提醒,从而更有效。为了保持简短,也有几种箭头函数的用途。如果您确实使用这个答案,请尝试阅读相关文档并了解:迭代器和生成器箭头函数表达式为了...的模板文字认真地,试着一步一步地思考。这就是你学习任何东西的方式。function* primes() {  const previous = [];  for (let i = 2; true; i++) {    let isPrime = true;    for (let p of previous) {      if (i % p === 0) {        isPrime = false;        break;      }    }    if (isPrime) {      previous.push(i);      yield i;    }  }}function* takeUntil(cb, iter) {  for (let val of iter) {    if (cb(val)) {      return;    }    yield val;  }}function showArrayIn(arr, container) {  arr.forEach(p => container.innerHTML += `${p},<br/>`); // technically, we used Array.forEach.}showArrayIn(  // get the prime number array declarativly   Array.from(takeUntil(n => n >= 50, primes())),  // show in the container specified  document.getElementById("results"));Primes:<div id="results"></div>

米琪卡哇伊

考虑以下示例。function isPrime(num) {&nbsp; if (num === 1) {&nbsp; &nbsp; return false;&nbsp; } else if (num === 2) {&nbsp; &nbsp; return true;&nbsp; } else {&nbsp; &nbsp; for (var x = 2; x < num; x++) {&nbsp; &nbsp; &nbsp; if (num % x === 0) {&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return true;&nbsp; }}function shown(n) {&nbsp; var list = [];&nbsp; for (var i = 1; i <= n; i++) {&nbsp; &nbsp; list.push(i);&nbsp; }&nbsp; list.slice().reverse().forEach(function(n, k, o) {&nbsp; &nbsp; if (!isPrime(n)) {&nbsp; &nbsp; &nbsp; list.splice(o.length - 1 - k, 1);&nbsp; &nbsp; }&nbsp; });&nbsp; document.getElementById("show").innerHTML = list;}shown(50);Prime: <p id="show"></p>

尚方宝剑之说

function primeFactorsTo(max){&nbsp; &nbsp; var store&nbsp; = [], i, j, primes = [];&nbsp; &nbsp; for (i = 2; i <= max; ++i)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (!store [i])&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; primes.push(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (j = i << 1; j <= max; j += i)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; store[j] = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return primes;}console.log(primeFactorsTo(5));console.log(primeFactorsTo(15));

ibeautiful

我认为这是我应得的正确答案..这是代码爱好者的简短而激进的function primes(limit){&nbsp; var prime=[], i=1;&nbsp; while (++i < limit+1) prime.reduce((a,c)=>(i%c)*a,1) && prime.push(i);&nbsp; prime.unshift(2);&nbsp; return prime;}[50].forEach(n=>document.getElementById('foreach').innerHTML=(`${primes(n)}`));
打开App,查看更多内容
随时随地看视频慕课网APP