# Factorialize a Number -- Proposed solutions need to take into account special case

I believe that the solutions offered to this challenge do not consider the special case where one tries to calculate the factorial of a negative number (it is not defined.) Here’s my solution:

``````function factorialize(num) {
let factor;
if (num < 0) {
factor = "undefined";
} else if (num == 0 | num == 1) {
factor = 1;
} else {
factor = num*factorialize(num - 1);
}
return factor;
}

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

The solutions offered do not consider the negative number case.

I edited the solution… It was wrong with for loop

Generally we avoid adding much input checking to the Guide solutions to keep them simpler.

I would avoid going through the if-else logic with a bunch of recursion, personally:

``````function factorialize(num) {
if (num < 0 || !Number.isInteger(num)) {
// Note: num! is well defined but
//  but not covered by the formula below
return undefined; // no need for "", this is a primative
} else {
let factorial = 1;
for (let i = 2; i <= num; i++) factorial *= i;
return factorial;
}
}

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

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