Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

print first n prime numbers in javascript

// first n prime numbers (divisble by: 1 and themselves only e.g. 2,3,5,7,11)
    const prime = (n) => {
      let arr = [];
      for (let i = 2; arr.length < n; i++) {
        var flag = 0;
        for (let k = 2; k <= i; k++) {
          if (i % k == 0 && i > k) {
            flag = 1; //if i is divisible by any small number than it is not prime number
          } else if (i % k == 0 && flag != 1) {
            arr.push(i);
          }
        }
      }
      return arr;
    };
    console.log(prime(5));
Comment

PREVIOUS NEXT
Code Example
Javascript :: nuxt 3 add plugin 
Javascript :: js spleep 
Javascript :: js blur element 
Javascript :: remove object from array by value javascript 
Javascript :: gitignore subfolders 
Javascript :: flatten nested array javascript 
Javascript :: find highest number in array javascript 
Javascript :: share to gmail from website 
Javascript :: JavaScript Finding HTML Element by Id 
Javascript :: parse json c# 
Javascript :: js date get hours 
Javascript :: angular 9 radio button checked 
Javascript :: javascript filter array multiple values 
Javascript :: new map js 
Javascript :: run code snippet 
Javascript :: axios post nuxt 
Javascript :: toastr options 
Javascript :: how to convert string to uppercase in javascript 
Javascript :: class constructor javascript 
Javascript :: nodejs http get request to external server 
Javascript :: inbuilt javascript functions for last word check 
Javascript :: date format date and time in js 
Javascript :: How do I redirect to another webpage using javascript 
Javascript :: cancel or abort axios request 
Javascript :: use appsettings.json in console app 
Javascript :: icomoon react native 
Javascript :: capitalize first letter of each word 
Javascript :: how to use empty href link in reactjs 
Javascript :: push values to state array class react 
Javascript :: how assign custom date to input type date in javascript 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =