Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

1

114
Views
How many prime numbers from 2 to 100000 javascript?

I am new to JS. I am trying to write a program where in I need to find the prime numbers between set of 2 numbers. When I run this on my IDE I get "Runtime Error - Other". No pressing debug on the IDE I get "Using long recursion is taking long"

Here is the program

var num1 = 2;
var num2 = 10;

var primeArray = [];

for(i=num1; i<=num2; i++){
    if(checkPrime(i)){
    primeArray.push(i)    
    }
}

console.log(primeArray.join(" "))

// console.log(checkPrime(10));

function checkPrime (num){
    //convert number to string and get each digit in string form
    var numString = num.toString().split("");
    //parse each string element to convert to number
    var numArray = numString.map(function(i){
        return parseFloat(i)
    });
    //sum all digits
    var sum = 0;
    for(i=0; i<numArray.length; i++){
        sum= sum+ numArray[i];
    }
    //2 is prime
    if(num == 2){
        return true;
    } else if(num%2 === 0){ //if even then not prime
        return false;
    } else if(sum == 3){ // 3 is prime
        return true;
    } else if(sum%3 === 0){ // if sum of all digits is divisible by 3 then not prime 
        return false;
    } else{
        return true;
    }
}  

I am not able to figure out where I am going wrong

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

var num1 = 2;
var num2 = 10;

var primeArray = [];

for(i=num1; i<=num2; i++){
    if(checkPrime(i)){
    primeArray.push(i)    
    }
}

console.log(primeArray.join(" "))

// console.log(checkPrime(10));

function checkPrime (num){
    //convert number to string and get each digit in string form
    var numString = num.toString().split("");
    //parse each string element to convert to number
    var numArray = numString.map(function(i){
        return parseFloat(i)
    });
    //sum all digits
    var sum = 0;
    for(i=0; i<numArray.length; i++){
        sum= sum+ numArray[i];
    }
    //2 is prime
    if(num == 2){
        return true;
    } else if(num%2 === 0){ //if even then not prime
        return false;
    } else if(sum == 3){ // 3 is prime
        return true;
    } else if(sum%3 === 0){ // if sum of all digits is divisible by 3 then not prime 
        return false;
    } else{
        return true;
    }
}  

about 4 years ago · 이가현 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!