Project Euler - Problem 3: Largest prime factor

Tell us what’s happening:
Describe your issue in detail here.
my code is running well but i can’t pass the challenge
why?
Your code so far

function largestPrimeFactor(number) {
  let n = 0;
  let s = true
  for(let i = 2; i < number; i++){
    if(number % i == 0){
      s = false
      n = number / i
      break;
    }
    } 
     if(s == false){
       largestPrimeFactor(n)
     }else{
       console.log(number)
       return number
     }
    
  }

largestPrimeFactor(13195);

Your browser information:

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

Challenge: Project Euler - Problem 3: Largest prime factor

Link to the challenge:

Are you failing on the last test?

:largestPrimeFactor(600851475143) should return 6857.

the last 3 for some reason
no idea why
it should work

Edit: I think if you change the last line to pass 8 to your code you can probably log what is happening.

But basically, for 8, you should get 2 back.

As for the bigger numbers you need a more efficient algorithm probably.

Your recursive case has no return value.

no. it’s giving 2
i tried every single number in the tests
i even tried it in vs code

it does in the last not here you can check it out try every number and it should log it out then it should return it

No. Your base case has a return value. But since your recursive case has no return value, that doesn’t get propegated up the call stack and you are returning ‘undefined’.

1 Like

ok. i will try it now

what should i do here? :confused:

Literally, add a return statement in the recursive case, right where I highlighted.

yeah you are right it is returning undefined
thank you
what do you think i should do here?
is my code invalid?
or can i fix it?
what should i return there?

Did you try this yet?

i tried that but what should i return?
i tried to return number and n and it’s still the same

you should return your function call.

without it the whole recursion won’t work

1 Like

oh ok
i just tried that and it returned another number :cactus:
i think my code needs more work
thank you sir

I was able to pass the tests with your code by adding literaaly one keyword to it.

I am not sure if your code needs some huge refactoring

1 Like

oh ok…
can you tell me what it is?
i won’t look at it now
im going to take a little break then try again
but if i wasn’t able to find what it is i will look here XD

You literally only need to add the return keyword.

1 Like

yeah, look at the above. I do not know how to give more hints and not to give working solution. :upside_down_face:

1 Like