Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript write all the prime numbers from 1 to 100

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

function display(n) {
    var arr = [2];
    for ( var i = 3; i < n; i+=2 ) {
        if ( isPrime(i) ) {
            arr.push(i);
        }
    }
    console.log(arr); // use arr result on your own
}

display(100);


//Added Functionality

function prime(n,flag) {
    ( typeof flag === "undefined" || flag === false ) ? flag = false : flag = true;

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

    if ( flag ) {
        var arr = [2];
        for ( var i = 3; i <= n; i+=2 ) {
            if ( isPrime(i) ) {
                arr.push(i);
            }
        }
        return arr;
    } else {
        return isPrime(n);
    }
}
Comment

prime numbers 1 to 100 in javascript

//Prime Number from 1 to 100
const arr = [] //Creating an Empty List for keeping all prime numbers
for(var p=2;p<=100;p++){ // 1 is not a prime number it is excluded and loop is started with 2 to 100.
  for(var i=2;i<p;i++ ){
            //2<2   ----->(Which is false program will return to last if)
    if(p%i == 0){ 													//|
      break															//|
    }																//|
  }																	//|
  if(p == i){   //Here and adds that number <-------------------------=
    arr.push(p)
  }
}
console.log(arr)
// Result :: [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to make hide/show btn in js 
Javascript :: toggle button by javascript 
Javascript :: js indexof regex 
Javascript :: sort by json fied in laravel 
Javascript :: javascript join 
Javascript :: ionic 3 alert 
Javascript :: convert array to object javascript 
Javascript :: react disable eslint errors 
Javascript :: add element to array javascript 
Javascript :: javascript get random number 
Javascript :: for htmlcollection javascript 
Javascript :: react save to local storage 
Javascript :: javascript detect if element is scrolled 
Javascript :: onclick on non button 
Javascript :: add key vakue to front of object 
Javascript :: error: expected undefined to be a graphql schema. 
Javascript :: how to make view dotted line in jsx 
Javascript :: send data from one page to another html page in Javascript 
Javascript :: local storage 
Javascript :: run a code after delay js 
Javascript :: Remove duplicate items form array using reduce() method. 
Javascript :: count number of duplicate pairs in array javascript 
Javascript :: moment timezone get timezone offset 
Javascript :: Both npm and yarn have created lockfiles for this application, 
Javascript :: how many days old am i 
Javascript :: disable scroll react 
Javascript :: lexical scoping javascript 
Javascript :: javascript print random word from list 
Javascript :: agregar clase en jquery 
Javascript :: make a get request in node js 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =