Problem 3: Largest prime factor

Problem 3: Largest prime factor
0

#1

Tell us what’s happening:

Your code so far


function largestPrimeFactor(number) {
  if(number==2 || number==3){
    return number;
  }
  var i,j,num,prime;
  for(i=4;i<=number;i++){
    num=1;
    //checking it is a prime number or not
    if(i%2!=0){var m=i/2;
    for(j=2;j<=m;j++){
      if(i%j==0){
        num=0;
        break;
        }
    }}
  if(num==1 && number%i==0){
      prime=i;
  }
}
document.write(prime);
  return prime;
}

largestPrimeFactor(13195);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/project-euler/problem-3-largest-prime-factor


#2

It works in codepen but is returning the wrong (too small) answer in the test. That probably means that it is timing out. It wants you to find a more efficient algorithm.