# Project Euler Problems 1 to 100 - Problem 3: Largest prime factor

I’m working on Problem 3 and for some reason my code (below) passes all the tests except that largestPrimeFactor (2) returns 2:

• Passed:`largestPrimeFactor(2)` should return a number.

• Failed:`largestPrimeFactor(2)` should return 2.

• Passed:`largestPrimeFactor(3)` should return 3.

• Passed:`largestPrimeFactor(5)` should return 5.

• Passed:`largestPrimeFactor(7)` should return 7.

• Passed:`largestPrimeFactor(8)` should return 2.

• Passed:`largestPrimeFactor(13195)` should return 29.

• Passed:`largestPrimeFactor(600851475143)` should return 6857.

I’m not sure what I’m doing wrong and am not seeing what part of my program is making it such that all the other tests work but not this particular 2 case - I even specified that 2 was a prime number, which helped with the “largestPrimeFactor(8) should return 2” test but not this one. Does anyone see the flaw in my code? Thanks!

``````function largestPrimeFactor(number) {
const factors3 = factor(number);
let i = 0;
while (i < factors3.length) {
if (isPrime(factors3[i]) == true) {
}
i += 1;
}
}

function factor(number) {
const factors = [];
for (let i = 1; i < Math.sqrt(number)+1; i++) {
if (number % i == 0) {
factors.push(i);
factors.push(number/i);
}
}
return factors
}

function isPrime(number) {
if (number == 2) {
return true
}
const factors2 = factor(number);
let size = factors2.length;
if (size == 2) {
return true;
}
else {
return false;
}
}

largestPrimeFactor(13195);
``````

What is then returned for `largestPrimeFactor(2)`? There isn’t smaller prime number than `2`. Try to figure out, why there might be any other number for this case that’s considered as a prime number.