Factorialize num potential solution

Tell us what’s happening:
This is the solution I came up with and I’m getting undefined. Can someone help and just tell me if I’m close or completely screwing it up?

Thank you!

Your code so far


function factorialize(num) {
for (let i = num-1; i >= 0; i--) 
if (num > 1) {
  num = num * [i]
}
else if (num === 0 || num === 1){
num = 1}
else if (num )
return num;
}

console.log(factorialize(5));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36.

Challenge: Factorialize a Number

Link to the challenge:

Hello~!

A few things I notice:

for (let i = num-1; i >= 0; i--) Here you are missing the opening curly brace, so your loop does not appear to do anything.

if (num > 1) {
  num = num * [i]
}

This block is going to catch you in an infinite loop. You’re multiplying num by i, and i is not going to be negative in your loop bounds.

Which means:

else if (num )
return num;

This block (also missing braces) will never execute, and your function never hits a return.

Thus the undefined - a function that doesn’t reach a return statement will implicitly return undefined.

you are multiplying a number with an array… It may works for type conversion, but just remove those square parenthesis

2 Likes