0

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

  • https://stackoverflow.com/questions/40200089/number-prime-test-in-javascript#:~:text=function%20isPrime(num)%20%7B%20if,log(isPrime(121))%3B – cmgchess Jan 30 '22 at 08:45
  • 4
    You seem to think that if a number cannot be divided by 2 nor 3, then it must be prime. I think you need to revisit that... – trincot Jan 30 '22 at 08:52

0 Answers0