1

Here's my code but my answer is not that which i want..

Please Check This and give me a solution to get prime number using Foreach Loop b/w 1-50

Thanks In Advance :)

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);
raoabdullah07
  • 356
  • 3
  • 13
  • 1
    Might try here first: https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions – Twisty Aug 20 '20 at 05:41
  • Welcome to Stack Overflow. What errors are you getting and what output are you expecting? – Twisty Aug 20 '20 at 05:43
  • I think you need to first logically break down your problem into small steps. Then, when you try solving the steps one-by-one, you can come up with more specific problem with, maybe, one specific step. And with that specific problem, you can probably find solution online already. – Koala Yeung Aug 20 '20 at 05:48
  • @twisty i just wants a prime numbers through forEach Loop – raoabdullah07 Aug 20 '20 at 06:08
  • @Gerard No Dear, i want to use forEach loop not for Loop – raoabdullah07 Aug 20 '20 at 06:09
  • @AbdullahRao a `.forEach()` loop on what array? – Twisty Aug 20 '20 at 06:28
  • @Twisty i have explained before in my question.. array is between 1 to 50 numbers – raoabdullah07 Aug 20 '20 at 06:39
  • @AbdullahRao your example does not show this. you have an array with a single element: `[2]`. – Twisty Aug 20 '20 at 06:47
  • but in shown(50) it shows that array starts with 2 and ends on 50 – raoabdullah07 Aug 20 '20 at 06:50
  • @AbdullahRao You just pasted your whole code here. There are many logical steps in it that you can verify one-by-one. Thay way you'd be able to submit a more specific question than this "I don't know how to do the whole thing" type question. – Koala Yeung Aug 26 '20 at 08:11

4 Answers4

1

This is probably a too-advanced answer for a homework of this level, but technically it follows the rules (use Array.forEach) and it works.

The primes() generates new primes based on previous primes. So it won't test the reminder of all integers, thus more effecient. There are several arrow function uses, too, to keep things short. If you indeed use this answer, please try to read the relevant documentations and learn:

Seriously, try to think step-by-step. That's how you learn anything.

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>
Koala Yeung
  • 7,475
  • 3
  • 30
  • 50
0
function primeFactorsTo(max)
{
    var store  = [], i, j, primes = [];
    for (i = 2; i <= max; ++i) 
    {
        if (!store [i]) 
          {
            primes.push(i);
            for (j = i << 1; j <= max; j += i) 
            {
                store[j] = true;
            }
        }
    }
    return primes;
}

console.log(primeFactorsTo(5));

console.log(primeFactorsTo(15));
0

Consider the following example.

function isPrime(num) {
  if (num === 1) {
    return false;
  } else if (num === 2) {
    return true;
  } else {
    for (var x = 2; x < num; x++) {
      if (num % x === 0) {
        return false;
      }
    }
    return true;
  }
}

function shown(n) {
  var list = [];
  for (var i = 1; i <= n; i++) {
    list.push(i);
  }
  list.slice().reverse().forEach(function(n, k, o) {
    if (!isPrime(n)) {
      list.splice(o.length - 1 - k, 1);
    }
  });
  document.getElementById("show").innerHTML = list;
}

shown(50);
Prime: <p id="show"></p>
Twisty
  • 30,304
  • 2
  • 26
  • 45
0

I Think so this is the correct answer which i deserve.. It is code lover short and aggressive

function primes(limit)
{
  var prime=[], i=1;
  while (++i < limit+1) prime.reduce((a,c)=>(i%c)*a,1) && prime.push(i);
  prime.unshift(2);
  return prime;
}
[50].forEach(n=>document.getElementById('foreach').innerHTML=(`${primes(n)}`));
raoabdullah07
  • 356
  • 3
  • 13