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 :: button in vanilla js 
Javascript :: how to turn a number negative in javascript 
Javascript :: node js variables in string 
Javascript :: how to convert kilomer to meter in javascript 
Javascript :: navigation.openDrawer is not a function react native 
Javascript :: ckeditor get value 
Javascript :: jquery array remove element 
Javascript :: Reading Time with jquery 
Javascript :: play audio with js 
Javascript :: send mail with javascript 
Javascript :: jquery sort listing alphabetically 
Javascript :: vue js reload page 
Javascript :: exit from fullscreen 
Javascript :: html button javascript void 
Javascript :: mongoose get raw 
Javascript :: Both npm and yarn have created lockfiles for this application, but only one can be used to install dependencies. 
Javascript :: js get object keys 
Javascript :: jquery ajax 500 error handling 
Javascript :: ant design table sort string perfectly 
Javascript :: express return json 
Javascript :: on mouse not over jquiery 
Javascript :: jquery get selected checkbox value array 
Javascript :: javascript is int in array 
Javascript :: function(a, b){return b - a} 
Javascript :: Getting Binary gap in JS 
Javascript :: scroll down div from component angular 
Javascript :: cube root of a number in js 
Javascript :: jquery thousand separator 
Javascript :: access key of object javascript 
Javascript :: react-native android build apk 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =