1function isPrime(num) {
2 for ( var i = 2; i < num; i++ ) {
3 if ( num % i === 0 ) {
4 return false;
5 }
6 }
7 return true;
8}
9
10function display(n) {
11 var arr = [2];
12 for ( var i = 3; i < n; i+=2 ) {
13 if ( isPrime(i) ) {
14 arr.push(i);
15 }
16 }
17 console.log(arr); // use arr result on your own
18}
19
20display(100);
21
22
23//Added Functionality
24
25function prime(n,flag) {
26 ( typeof flag === "undefined" || flag === false ) ? flag = false : flag = true;
27
28 function isPrime(num) {
29 if ( num === 0 || num === 1 ) {
30 return false;
31 }
32 for ( var i = 2; i < num; i++ ) {
33 if ( num % i === 0 ) {
34 return false;
35 }
36 }
37 return true;
38 }
39
40 if ( flag ) {
41 var arr = [2];
42 for ( var i = 3; i <= n; i+=2 ) {
43 if ( isPrime(i) ) {
44 arr.push(i);
45 }
46 }
47 return arr;
48 } else {
49 return isPrime(n);
50 }
51}
52