Stuck with factorialize, don´t know where the bug is

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

function factorialize(num) {
let count = num
let result = 1

if (num === 0) {
  result = 1
} else {
    while (count >= 0) {
      result *= count
      count--
    }
}


return result;
}

factorialize(5);
console.log(factorialize(5))
  **Your browser information:**

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

Challenge: Factorialize a Number

Link to the challenge:

Hi folks!

I am stuck with this challenge. I know there are other ways to do it, but I´d be happy to understand why the above solution won´t work.

Hello!
It’s time to do a little debugging. Try following your code for any number and see what it does line by line.
You have a condition that guarantees that 0 will be returned except for factorialize(0).

2 Likes
while (count >= 1)

Thanks for the hint, placed a console.log() in front of return line, that helped.

I´m a bit surprised though, that this solution is not among those presented in the “Get a hint” - section.

Even more, after solving the challenges up to “Basic Data Structures”, I can see no way that I could´ve come up with any of these solutions by myself, as from my perspective they seem more abstract and need more knowhow than was touched before.

Is this a fact I should be worried about? Also, is the way I solved it overly complicated?

Thanks for the feedback!

Hei!

This all could be solved like this:

function factorialize(num) {
let result= 1;
    while (num> 0) {
      result *= num
      num--
    }
return result;
}

factorialize(5);
console.log(factorialize(5))

Also when it comes to solving algorithms, practice is what really matters IMO. After a while, you should get the hang of it.

1 Like

The recursive functions might be a bit much - but the for-loop should be doable.
What you did is not overly complicated, but a bit.
First off, you don’t need “count” because all you do to it could be done with “num”.
Then ofcourse if you can easily count something up/down, then a for-loop is the preferred choice compared to a while-loop.

2 Likes

Yes, after diving into it the for-loop approach is plausible as well. Struggled with

let i = 2

until it suddenly dawned on me that it could also be equal to 1 and would still work.

I think I also get the first recursion approach. I suppose the function is passed to itself with diminishing input until it reaches the terminal condition. (Question here: Is a recursive function per se an iterative action?)

At no3 it get´s “blurry”, and no4 is just out of space for now…

Thanks for all the feedback so far, this is helping a lot!

Recursion is used when a function calls itself again until it reaches a stop case. It’s called iteration when you use some form of repetition(for/while)
Also, you can see the recursive approach as a stack that is gradually filled with each new call. That’s why you get a stack overflow if you don’t set the terminal condition properly.

1 Like

Hello liqu_it
Well there are lots of unnecessary complexities on your code like declaring count and result.
As for this challenge just use recursion it will not be hard.
function factorialize(num) { if(num < 0){ return } if(num < 2){ return 1 } if(num > 0){ return num * factorialize(num - 1) } }

1 Like

Thanks for all the input guys. Supposedly I´m at the point where the learning curve eventually is steepening. Still figuring out whether this stuff is something for me, on the one hand I´m stoked when I finally get a new concept, on the other I find it increasingly difficult to apply those concepts on blank paper. Guess this is where the wheat is sorted from the chaff :smiley:

I believe that it’s all about your expectations. You need to be realistic. If you expect to solve an algorithm in under 30 minutes no matter what difficulty then you are setting yourself up for disappointment. The same goes for learning front-end/back-end development.
Sometimes I have to think about a concept or a problem for more than a week before I can kind of grasp it. So don’t be discouraged. The more you work the better it gets(most of the time :upside_down_face:).

1 Like

Impatience is indeed a problem once in a (every) while, so a more levelheaded approach might actually help a lot! :smile: Thanks for the kind words!

1 Like

One thing to keep in mind, in most cases you probably don’t want to use a while loop. A for loop is more common.

2 Likes

Thanks for the cue. I´ll have a deeper look into where each of them has advantages.

The general rule of thumb is that if you can use a for loop, then you probably shouldn’t be using a while loop.

1 Like

That´s easy to remember, thanks! :grin:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.